#include <bits/stdc++.h>

using namespace std;
const long long LMAX = 9223372036854775807;
int n,m;
int main()
{
    cin>>n>>m;
    int niz[n][m];
    long long resenje[n][m];
    for(int i = 0; i<n; i++){
        for(int j = 0; j<m; j++){
            cin>>niz[i][j];
        }
    }
    int q;
    cin>>q;
    queue<long long> ispis;
    for(int i = 0; i<q; i++){
        int x,y;
        cin>>x>>y;
        x--;y--;
        resenje[0][0] = niz[0][0];
        for(int i = 1; i<n; i++){
            if(!((i-1==x&&0==y)||resenje[i-1][0]==LMAX)){
                resenje[i][0] = resenje[i-1][0]+niz[i][0];
            }
            else{
                resenje[i][0] = LMAX;
            }
        }
        for(int i = 1; i<m; i++){
            if(!((i==y&&0==x)||resenje[0][i-1]==LMAX)){
                resenje[0][i] = resenje[0][i-1]+niz[0][i];
            }
            else{
                resenje[0][i] = LMAX;
            }
        }
        for(int i = 1; i<n; i++){
            for(int j= 1; j<m; j++){
                if(!(i-1==x&&j==y)&&!(i==x&&j-1==y)){
                    resenje[i][j] = min(resenje[i-1][j],resenje[i][j-1])+niz[i][j];
                }
                else if(i-1==x&&j==y){
                    resenje[i][j] = resenje[i][j-1]+niz[i][j];
                }
                else{
                    resenje[i][j] = resenje[i-1][j]+niz[i][j];
                }
            }

        }
        ispis.push(resenje[n-1][m-1]);
    }
    while(!ispis.empty()){
        cout<<ispis.front()<<"\n";
        ispis.pop();
    }
    return 0;

}