#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> doha(n);
    vector<vector<int>> duzine(n);
    int a;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> a;
            doha[i].push_back(a);
        }
        duzine[i] = vector<int>(m);
    }
    int QQ;
    cin >> QQ;
    vector<int> RESENJA(QQ);
    int banI, banJ, mini;
    stack< pair<int, int> > q;
    for(int iq = 0; iq < QQ; iq++){
        duzine[0][0] = doha[0][0];
        q.push( {0, 0} );

        cin >> banI >> banJ;
        banI--;
        banJ--;
        mini = -1;

        while(!q.empty()){
            pair<int,int> indeks = q.top();
            int inI = indeks.first;
            int inJ = indeks.second;
            q.pop();
            if(duzine[inI][inJ] < mini || mini == -1){
                if(inI != n-1 && !(inI+1 == banI && inJ == banJ) ){
                    q.push( {inI+1, inJ} );
                    duzine[inI+1][inJ] = duzine[inI][inJ]+doha[inI+1][inJ];
                }
                if(inJ != m-1 && !(inI == banI && inJ+1 == banJ) ){
                    q.push( {inI, inJ+1} );
                    duzine[inI][inJ+1] = duzine[inI][inJ]+doha[inI][inJ+1];
                }
                if(inI == n-1 && inJ == m-1){
                   mini = duzine[inI][inJ];
                }
            }
        }
        RESENJA[iq] = mini;
    }
    for(int i = 0; i < QQ; i++){
        cout << RESENJA[i] << endl;
    }
}