#include <iostream>
#include <algorithm>
#include <climits> 

using namespace std;

const unsigned int MAXN = 1000;
unsigned int dp[MAXN][MAXN];



int funkcija(unsigned int costs[MAXN][MAXN],unsigned int m,unsigned int n, unsigned int x,unsigned int y){
    costs[x][y] = INT_MAX-1000;
 
    dp[1][1] = costs[1][1];

    for (int j = 2; j <= m; j++)
    {
        dp[1][j] = dp[1][j - 1] + costs[1][j];
    }
    

    for (int i = 2; i <= n; i++)
    {
        dp[i][1] = dp[i - 1][1] + costs[i][1];
    }

    for (int i = 2; i <= n; i++)
    {
        for (int j = 2; j <= m; j++)
        {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + costs[i][j];
        }
    }
    return dp[n][m];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    unsigned int M,N,x[1000],y[1000];cin>>N>>M;
    unsigned int Q;
    unsigned int A[MAXN][MAXN];

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= M; j++)
        {
          
            cin>>A[i][j];
        }
    }
    cin>>Q;
    
    for(int i=0; i<Q;i++){
        cin>>x[i]>>y[i];
    }

    for(int i=0; i<Q;i++){
        unsigned int tmp= A[x[i]][y[i]];
        cout<<funkcija(A,M,N,x[i],y[i])<<endl;
        A[x[i]][y[i]]= tmp;
    }
    

    return 0;
}