#include <iostream>
using namespace std;

typedef unsigned long long int lli;

int dozvoljenminimalni(lli a, lli b)
{
    if (a<=0) 
    {
        return b;
    }
    else if (b<=0) 
    {
        return a;
    }
    else if (a<b) 
    {
        return a;
    }
    return b;
}

int main(){
    lli n,m,q,kolona,brq1,brq2,br;
    cin >> n >> m;
    lli matrica[n][m];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin >> br;
            matrica[i][j] = br;
        }
    }
    cin >> q;
    lli matricq[q][2];
    for(int i=0;i<q;i++)
    {
        cin >> brq1 >> brq2;
        matricq[i][0] = brq1-1;
        matricq[i][1] = brq2-1;
    }
    lli matrica2[n][m];
    matrica2[0][0] = matrica[0][0];
    for (int i=0;i<q;i++)
    {
        for (int dig=1;dig<n+m-1;dig++)
        {
            for (int red=0;red<n;red++)
            {
                kolona = dig - red;
                if (kolona>=0 && kolona<m)
                {
                    if (matricq[i][0] == red && matricq[i][1] == kolona)
                    {
                        matrica2[red][kolona] = -1;
                    } 
                    else 
                    {
                        if (kolona == 0){
                            if (matrica2[red-1][0] == -1)
                            {
                                matrica2[red][0] = -1;
                            } 
                            else
                            {
                                matrica2[red][0] = matrica2[red-1][0] + matrica[red][0];
                            }
                        } 
                        else if (red == 0)
                        {
                            if (matrica2[0][kolona-1] == -1){
                                matrica2[0][kolona] = -1;
                            } 
                            else 
                            {
                                matrica2[0][kolona] = matrica[0][kolona] + matrica2[0][kolona-1];
                            }
                        } 
                        else 
                        {
                            matrica2[red][kolona] = dozvoljenminimalni(matrica2[red][kolona-1], matrica2[red-1][kolona]) + matrica[red][kolona];
                        }
                    }
                }
            }
        }
        cout << matrica2[n-1][m-1] << "\n";
    }
}