#include <iostream>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    long long a[n+2][m+2];
    for (int i=0; i<=n+1; ++i){
        for (int j=0; j<=m+1; ++j){
            if (i*j>0 && i<=n && j<=m){cin>>a[i][j];}
            else{a[i][j]=1000000002;}
        }
    }
    long long pm[m+2][n+2];
    pm[1][1]=a[1][1];
    for (int i=0; i<=n; ++i){
        for (int j=0; j<=m; ++j){
            if (i*j==0){pm[i][j]=1000000000000007;}
            else if (i*j>1){
                pm[i][j]=a[i][j]+min(pm[i-1][j], pm[i][j-1]);
            }
            else if (i==1 && j>1){pm[i][j]=pm[i][j-1]+a[i][j];}
            else if (i>1 && j==1){pm[i][j]=pm[i-1][j]+a[i][j];}
        }
    }
    pair<int,int> w[m+n];
    w[0]=make_pair(m,n);
    int b=m+n;
    int trr=n;
    int trk=m;
    while (b>0){
        if (pm[trr-1][trk]<pm[trr][trk-1]){
            w[m+n-b]=make_pair(trr-1, trk);
            trr--;
        }
        else{
            w[m+n-b]=make_pair(trr, trk-1);
            trk--;
        }
        b--;
    }
    int q;
    cin>>q;
    for (int z=0; z<q; ++z){
        int lr, lk;
        cin>>lr>>lk;
        pair<int,int> xzxz=make_pair(lr,lk);
        bool naputu=false;
        for (int i=0; i<m+n && naputu==false; ++i){
            if (xzxz==w[i]){
                naputu=true;
            }
        }
        if (naputu==false){
            cout<<pm[n][m]<<endl;
        }
        else{
            long long ap[n+2][m+2];
            memcpy(ap, a, sizeof(a));
            ap[lr][lk]=100000000000000007;
            long long pmp[n+2][m+2];
            pmp[1][1]=ap[1][1];
            for (int i=0; i<=n; ++i){
                for (int j=0; j<=m; ++j){
                    if (i*j==0){pmp[i][j]=1000000000000007;}
                    else if (i*j>1){
                        pmp[i][j]=ap[i][j]+min(pmp[i-1][j], pmp[i][j-1]);
                    }
                    else if (i==1 && j>1){pmp[i][j]=pmp[i][j-1]+ap[i][j];}
                    else if (i>1 && j==1){pmp[i][j]=pmp[i-1][j]+ap[i][j];}
                }
            }
            cout<<pmp[n][m]<<endl;
        }
    }
    return 0;
}