#include <iostream>
#define MAXN 100
#define MAXM 100

using namespace std;

long long int guzva(long long int doha[MAXN][MAXM], unsigned int n, unsigned int m)
{
    for(unsigned int i = 1; i < n; i++)
    {
        doha[i][0] += doha[i - 1][0];
    }
    for(unsigned int j = 1; j < m; j++)
    {
            doha[0][j] += doha[0][j - 1];
    }
    for(unsigned int i = 1; i < n; i++)
    {
        for(unsigned int j = 1; j < m; j++)
        {
            doha[i][j] += min(doha[i - 1][j], doha[i][j - 1]);
        }
    }
    return doha[n - 1][m - 1];
}

void kopiraj(long long int a[MAXN][MAXM], long long int b[MAXN][MAXM], unsigned int n, unsigned int m)
{
    for(unsigned int i = 0; i < n; i++)
    {
        for(unsigned int j = 0; j < m; j++)
        {
            b[i][j] = a[i][j];
        }
    }
}

int main()
{
    unsigned int n, m, q;
    cin >> n >> m;
    long long int doha[MAXN][MAXM], dohaCopy[MAXN][MAXM];
    for(unsigned int i = 0; i < n; i++)
    {
        for(unsigned int j = 0; j < m; j++)
        {
            cin >> doha[i][j];
        }
    }
    cin >> q;
    while(q--)
    {
        kopiraj(doha, dohaCopy, n, m);
        unsigned int x, y;
        cin >> x >> y;
        dohaCopy[x - 1][y - 1] = 1000000001LL;
        cout << guzva(dohaCopy, n, m) << endl;
    }
    return 0;
}