#include <iostream>
#define maxN 1000000000
using namespace std;

int main()
{
    int n,m; cin>>n>>m;
    int a[n+1][m+1];
    int dp[n+1][m+1];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
            dp[i][j]=0;
        }
    }
    int q; cin>>q;
    for(int qi=0;qi<q;qi++)
    {
        int indi,indj;
        cin>>indi>>indj;

        int a2=a[indi][indj];

        a[indi][indj]=maxN;

        dp[1][1]=a[1][1];
        for (int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(i-1>0 && j-1>0)
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j];
                else if(i-1>0)
                    dp[i][j] = dp[i - 1][j] + a[i][j];
                else if(j-1>0)
                    dp[i][j] = dp[i][j-1] + a[i][j];
            }
        }
        a[indi][indj] = a2;

        cout<<dp[n][m]<<endl;
    }
    return 0;
}