#include <bits/stdc++.h>
using namespace std;

typedef tuple<int,int,int> cvor;

unsigned long long minPut(int n,int m,int x,int y,const vector<vector<int>>& put){
    vector<vector<bool>> posecen(n,vector<bool>(m));
    priority_queue<cvor,vector<cvor>,greater<cvor>> red;
    red.emplace(put[0][0],0,0);
    int a,b;
    unsigned long long r;
    while(!red.empty()){
        tie(r,a,b) = red.top();
        red.pop();
        if(a == n-1 && b == m-1) return r;
        if(posecen[a][b]) continue;
        posecen[a][b] = true;
        if((b < m-1) && (a != x || b+1 != y) && !posecen[a][b+1]) red.emplace(r+put[a][b+1],a,b+1);
        if((a < n-1) && (a+1 != x || b != y) && !posecen[a+1][b])red.emplace(r+put[a+1][b],a+1,b);
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;
    vector<vector<int>> put(n,vector<int>(m));
    for(int i = 0;i < n;i++){
        for(int j = 0;j < m;j++){
            cin >> put[i][j];
        }
    }
    int q;cin >> q;
    while(q--){
        int x,y;
        cin >> x >> y;x--;y--;
        cout << minPut(n,m,x,y,put) << "\n";
    }
}