#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int ma =10000;
ll matrica[ma][ma];
ll a[ma][ma];
int resi(ll matrica[][ma], int N, int M,int x, int y) {
    int t = matrica[x][y];
    matrica[x][y]=INT_MAX;
    a[1][1] = matrica[1][1];
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (i == 1 && j == 1) continue;
            else if (i == 1) a[i][j] = matrica[i][j] + a[i][j-1];
            else if (j == 1) a[i][j] = matrica[i][j] + a[i-1][j];
            else a[i][j] = matrica[i][j] + min(a[i][j-1], a[i-1][j]);
        }
    }
    matrica[x][y]=t;
    return a[N][M];
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int N, M;
    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            cin >> matrica[i][j];
        }
    }
    int Q;
    cin>>Q;
    for(int i =1;i<=Q;i++){
        int x,y;
        cin>>x>>y;
        cout << resi(matrica,N,M,x,y) << endl;
    }
    return 0;
}