#include <bits/stdc++.h>

using namespace std;

void f(vector<vector<long long>> a, int x, int y, int n, int m){
    a[x][y] = pow(10, 9);
    for (int i = 1; i < n; i++){
        a[i][0] += a[i - 1][0];
    }
    for (int i = 1; i < m; i++){
        a[0][i] += a[0][i - 1];
    }
    for (int i = 1; i < n; i++){
        for (int j = 1; j < m; j++){
            if (a[i - 1][j] < a[i][j - 1])
                a[i][j] += a[i - 1][j];
            else a[i][j] += a[i][j - 1];
        }
    }
    cout << a[n - 1][m - 1] << endl;
}

int main()
{
    int n, m, q;
    cin >> n >> m;
    vector<vector<long long>> a(n);
    for (int i = 0; i < n; i++){
        a[i].resize(m);
        for (int j = 0; j < m; j++){
            cin >> a[i][j];
        }
    }
    cin >> q;
    for (int i = 0; i < q; i++){
        int x, y;
        cin >> x >> y;
        f(a, x - 1, y - 1, n, m);
    }
	return 0;
}