#include<bits/stdc++.h>
using namespace std;
int n,m,q,bx,by,x,y,temp,navN;
int* doha;
int& dohaNa(int x,int y){
    if(x>n||y>m)
        return INT_MAX;
    return doha[x+y*n];
}
int main(){
    cin>>n>>m;
    daha=new int[n*(long long)m];
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            cin>>dohaNa(j,i);
    cin>>q;
    while(q--){
        cin>>xb>>yb;
        temp=dohaNa(xb,yb);
        dohaNa(x,y)=INT_MAX;
        x=1;
        y=1;
        navN=dohaNa(1,1);
        while(x!=n||y!=m){
            if(dohaNa(x+1,y)>dohaNa(x,y+1))
                y++;
            else
                x++;
            navN+=dohaNa(x,y);
        }
        cout<<navN<<endl;
        dohaNa(x,y)=temp;
    }
    return 0;
}