#include <iostream>
#include <vector>
using namespace std;


long long int minPathSum(vector<vector<long long int> >& grid, long long int n1, long long int m1) {
        long long int m = grid.size()-1;
        long long int n = grid[0].size()-1;
            vector<long long int> cur(m, grid[0][0]);

            for (long long int i = 1; i < m; i++)
                cur[i] = cur[i - 1] + grid[i][0];

            for (long long int j = 1; j < n; j++) {
                cur[0] += grid[0][j];
                for (long long int i = 1; i < m; i++)
                    cur[i] = min(cur[i - 1], cur[i]) + grid[i][j];
            }
            if((m1 == n1) || m1 < n1)
                return cur[m-1];
            else
                return cur[m-2];


    }

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    long long int n,m;

    cin >> n >> m;
    long long int storage[n][m];
    long long int num;
    vector<vector<long long int> > doha(m+1);
    if(m < n)
        doha.resize(n+1);

         for(unsigned long long int i = 0; i < n; i++)
        {
            for(unsigned long long int j = 0; j < m; j++)
            {

                cin >> num;
                storage[i][j]=0;
                doha[i].push_back(num);

            }
        }

        for(long long int i=0; i < n+1; i++){
        doha[i].push_back(0);

        }
        for(long long int i=0; i < m+1; i++){
            doha[n].push_back(0);

        }

    long long int q;
    cin >> q;
    long long int x1,y1;

    for(unsigned long long int i =0 ; i < q; i++)
    {
        cin >> x1 >> y1;
        long long int x = x1-1;
        long long int y = y1-1;
        long long int temp = doha[x][y];
        long long int res=0;
        doha[x][y] = 1e17;
        if(storage[x][y] != 0)
            res = storage[x][y];
        else{
            res = minPathSum(doha,n,m);
            storage[x][y] = res;
        }
        cout << res << '\n';
        doha[x][y] = temp;

    }
    return 0;
}