#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i = (int)(a); i <= (int)(b); i++)
#define RF(i,a,b) for(int i = (int)(a); i >= (int)(b); i--)
int main()
{
    long X,Y,Q,_i,_j,resenja[10000];
    cin>>X>>Y;
    long Cost[X][Y];

    F(i,0,X-1)
    {
        F(j,0,Y-1)
        {
            cin>>Cost[i][j];
        }
    }
    cin>>Q;
    
    for(int k=0; k<Q; k++){
    cin>>_i>>_j;
    int pom=Cost[_i][_j];
    Cost[_i][_j]=10000;
    int MinCost[X][Y];
    MinCost[0][0] = Cost[0][0];
    F(j,1,Y-1)
        MinCost[0][j] = MinCost[0][j-1] + Cost[0][j];
    F(i,1,X-1)
        MinCost[i][0] = MinCost[i-1][0] + Cost[i][0];
    F(i,1,X-1)
    {
        F(j,1,Y-1)
        {
            MinCost[i][j] = min(MinCost[i-1][j],MinCost[i][j-1]) + Cost[i][j];
        }
    }
    
    resenja[k]=MinCost[X-1][Y-1];
    Cost[_i][_j]=pom;
    }
    for(int i=0; i<Q; i++){
        cout<<resenja[i]<<endl;
    }
    
    cin>>X;
    return 0;
}