#include <iostream>
#define BLOCKED 1000000001

using namespace std;

int main() {
    int N, M;
    cin >> N >> M;
    int grid[N][M];
    int dp[N][M];

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            cin >> grid[i][j];
        }
    }

    int Q;
    cin >> Q;
    int a[Q], b[Q];
    for (int q = 0; q < Q; ++q) {
        cin >> a[q] >> b[q];
        --a[q];
        --b[q];
    }

    int temp, t, l, ans[Q];
    for (int q = 0; q < Q; ++q) {
        temp = grid[a[q]][b[q]];
        grid[a[q]][b[q]] = BLOCKED;

        dp[0][0] = grid[0][0];

        for (int j = 1; j < M; ++j) {
            dp[0][j] = dp[0][j-1] + grid[0][j];
        }

        for (int i = 1; i < N; ++i) {
            dp[i][0] = dp[i-1][0] + grid[i][0];
        }

        for (int i = 1; i < N; ++i) {
            for (int j = 1; j < M; ++j) {
                t = dp[i-1][j], l = dp[i][j-1];
                if (t < l) {
                    dp[i][j] = t + grid[i][j];
                }
                else {
                    dp[i][j] = l + grid[i][j];
                }
            }
        }

        grid[a[q]][b[q]] = temp;
        ans[q] = dp[N-1][M-1];
    }

    for (int q = 0; q < Q; ++q) {
        cout << ans[q] << "\n";
    }

    return 0;
}