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

int minZbir(const vector<vector<int>>& M,int n, int m) {
    vector<int> dp(n);
    dp[0] = M[0][0];
    for (int k = 1; k < m; k++)
        dp[k] = dp[k-1] + M[0][k];
    for (int v = 1; v < n; v++) {
        dp[0] += M[v][0];
            for (int k = 1; k < m; k++){
                dp[k] = min(dp[k] + M[v][k], dp[k-1] + M[v][k]);

            }
    }
    return dp[m-1];
}

int main() {
    int n,m;
    cin >> n >> m;
    vector<vector<int>> M(n);
    for (int i = 0; i < n; i++) {
        M[i].resize(m);
        for (int j = 0; j < m; j++)
            cin >> M[i][j];
    }
    int q;
    cin >> q;
    for(int i=0;i<q;i++){
        int x,y;
        cin >> x >> y;
        x--;
        y--;
        int broj=M[x][y];
        M[x][y]=1e9;
        cout << minZbir(M,n,m) << endl;
        M[x][y]=broj;
    }

    return 0;
}