#include <iostream>

using namespace std;

int main()
{
    long long int n, m, i, j, q, x, y;
    cin >> n >> m;
    long long int matrica[n+1][m+1];
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin >> matrica[i][j];
        }
    }


    //cout << endl;for(i=1; i<=n; i++){for(j=1; j<=m; j++){cout << b[i][j] << " ";}cout << endl;}cout << endl;

    cin >> q;
    while(q--)
    {
        cin >> x >> y;
        long long int axy;

        axy = matrica[x][y];
        matrica[x][y] = LONG_MAX;

        long long int b[n+1][m+1];
        b[1][1]=matrica[1][1];
        for(j=2; j<=m; j++)
        {
            b[1][j] = b[1][j-1] + matrica[1][j];
        }
        for(i=2; i<=n; i++)
        {
            b[i][1] = b[i-1][1] + matrica[i][1];
        }
        for(i=2; i<=n; i++)
        {
            for(j=2; j<=m; j++)
            {
                b[i][j] = min(b[i-1][j], b[i][j-1]) + matrica[i][j];
            }
        }

        cout << b[n][m] << endl;
        matrica[x][y] = axy;
    }
}