#include <iostream>
#include <vector>

using namespace std;

int n,m,q,x,y;

vector<vector<int>> matrica;

long long minara(long long a, long long b)
{
    if(a<b)
        return a;
    return b;
}

long long rijesi()
{
    int i,j;
    vector<vector<long long>> dp;
    dp.resize(n);
    dp[0].push_back(matrica[0][0]);
    for(i=1; i<n; i++)
        if(dp[i-1][0]==-1 || matrica[i][0]==-1)
            //dp[i][0]=-1;
            dp[i].push_back(-1);
        else
            //dp[i][0]=dp[i-1][0]+matrica[i][0];
            dp[i].push_back(dp[i-1][0]+matrica[i][0]);

    for(j=1; j<m; j++)
        if(dp[0][j-1]==-1 || matrica[0][j]==-1)
            //dp[0][j]=-1;
            dp[0].push_back(-1);
        else
            //dp[0][j]=dp[0][j-1]+matrica[0][j];
            dp[0].push_back(dp[0][j-1]+matrica[0][j]);

    for(i=1; i<n; i++)
        for(j=1; j<m; j++)
        {
            if(matrica[i][j]==-1)
            {
                //dp[i][j]=-1;
                dp[i].push_back(-1);
                continue;
            }
            long long gore=dp[i-1][j];
            long long levo=dp[i][j-1];
            if(gore==-1)
                //dp[i][j]=matrica[i][j]+levo;
                dp[i].push_back(matrica[i][j]+levo);
            else if(levo==-1)
                //dp[i][j]=matrica[i][j]+gore;
                dp[i].push_back(matrica[i][j]+gore);
            else
                //dp[i][j]=minara(matrica[i][j]+gore, matrica[i][j]+levo);
                dp[i].push_back(minara(matrica[i][j]+gore, matrica[i][j]+levo));
        }
    return dp[n-1][m-1];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>m;
    matrica.resize(n);
    int i,j;
    for(i=0; i<n; i++)
        for(j=0; j<m; j++)
        {
            cin>>q;
            matrica[i].push_back(q);
        }
    cin>>q;
    while(q--)
    {
        cin>>x>>y;
        x--;
        y--;
        int tmp=matrica[x][y];
        matrica[x][y]=-1;
        cout<<rijesi()<<"\n";
        matrica[x][y]=tmp;
    }

    return 0;
}