#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    uint32_t N, M, a, Q;
    cin >> N >> M;
    vector<vector<uint_fast64_t>> navijaci(N);
    vector<vector<uint_fast64_t>> tempnavijaci;
    vector<int> X;
    vector<int> Y;
    for(int i = 0; i < N; i++){
        navijaci[i].resize(M);
        for(int j = 0; j < M; j++){
            cin >> a;
            navijaci[i][j] = a;
        }
    }
    copy(navijaci.begin(), navijaci.end(), back_inserter(tempnavijaci));
    cin >> Q;
    for(int i = 0; i < Q; i++){
        int x, y;
        cin >> x >> y;
        X.push_back(x - 1);
        Y.push_back(y - 1);
    }


    for(int k = 0; k < Q; k++){

        int x = X[k]; int y = Y[k];
        navijaci[x][y] = 1000000001;

        for(int i = 1; i < N; i++){
            if(navijaci[i - 1][0] == navijaci[x][y]){
                navijaci[i][0] = navijaci[x][y];
                continue;
            }
            navijaci[i][0] += navijaci[i - 1][0];
        }
        for(int j = 1; j < M; j++){
            if(navijaci[0][j - 1] == navijaci[x][y]){
               navijaci[0][j] = navijaci[x][y];
               continue;
            }
            navijaci[0][j] += navijaci[0][j - 1];
        }
        for(int j = 1; j < M; j++){
            for(int i = 1; i < N; i++){
                if(navijaci[i][j] == navijaci[x][y]){
                    continue;
                }
                if(navijaci[i][j - 1] > navijaci[i - 1][j]){
                    navijaci[i][j] += navijaci[i - 1][j];
                }
                else{
                    navijaci[i][j] += navijaci[i][j - 1];
                }
            }
        }
        cout << navijaci[N - 1][M - 1] << endl;
        navijaci.clear();
        copy(tempnavijaci.begin(), tempnavijaci.end(), back_inserter(navijaci));
    }


    return 0;
}