#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long int ull;
typedef vector<ull> VULL;
typedef vector<VULL> VVULL;

void unos(VVULL& v, ull G1, ull G2){
    for(int i = 0; i < G1; i++) for(int j = 0; j < G2; j++) cin >> v[i][j];
}

ull najboljiPut(const VVULL& referenca, ull G1, ull G2){
    VVULL BT(G1, VULL(G2));
    for(ull i = 0; i < G1; i++){
        for(ull j = 0; j < G2; j++){
            if(i == 0 && j == 0) BT[i][j] = referenca[i][j];
            else {
                if(i != 0 && j !=0 ) BT[i][j] = referenca[i][j] + min(BT[i-1][j], BT[i][j-1]);
                if(i == 0) BT[i][j] = referenca[i][j] + BT[i][j-1]; 
                else if(j == 0) BT[i][j] = referenca[i][j] + BT[i-1][j];
            }
        }
    }
    return BT[G1 - 1][G2 - 1];
}

int main(int argc, char **argv){

    ull N, M, blokX, blokY, mem, Q;
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> N >> M; 

    VVULL Doha(N, vector<ull>(M));

    unos(Doha, N, M);   

    cin >> Q;
    for(int r = 0; r < Q; r++){
        cin >> blokX >> blokY;

        --blokX; --blokY;

        mem = Doha[blokX][blokY];

        Doha[blokX][blokY] = 1e10 + 1;

        cout << najboljiPut(Doha, N, M)<< '\n';

        Doha[blokX][blokY] = mem;
    }
}