#include <bits/stdc++.h>
int n,m,q,blokx,bloky,resenj;int a[1005][1005];int b[1005][1005];
using namespace std;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
        cin>>a[i][j], b[i][j]=a[i][j];
    cin>>q;
    for(int t=1;t<=q;t++){
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            a[i][j]=b[i][j];
        cin>>blokx>>bloky;
        a[blokx][bloky]=1000000009;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(i-1!=0 and j-1!=0)
                a[i][j]+=min(a[i-1][j],a[i][j-1]);
            else if(i-1==0 and j-1!=0)
                a[i][j]+=a[i][j-1];
            else if(i-1!=0 and j-1==0)
                a[i][j]+=a[i-1][j];
            else continue;
            resenj=a[i][j];
        }
        cout<<resenj<<endl;
    }
   
    return 0;
}