#include<bits/stdc++.h>
using namespace std;
int redN,kolN,q,redB,kolB,red,kol,temp,navN;
int* doha;
int& dohaNa(int redK,int kolK){
    redK--;
    kolK--;
    static int tmp;
    if(redK>=redN||kolK>=kolN)
        return tmp=INT_MAX;
    return doha[kolN*redK+kolK];
}
int main(){
    cin>>redN>>kolN;
    doha=new int[redN*(long long)kolN];
    for(red=1;red<=redN;red++)
        for(kol=1;kol<=kolN;kol++)
            cin>>dohaNa(red,kol);
    cin>>q;
    while(q--){
        cin>>redB>>kolB;
        temp=dohaNa(redB,kolB);
        dohaNa(redB,kolB)=INT_MAX;
        red=1;
        kol=1;
        navN=dohaNa(1,1);
        while(red!=redN||kol!=kolN){
            if(dohaNa(red+1,kol)<dohaNa(red,kol+1)||((kol+1!=kolN||red!=redN)&&(dohaNa(red+1,kol+1)==INT_MAX)&&dohaNa(red,kol+2)==INT_MAX))
                red++;
            else
                kol++;
            navN+=dohaNa(red,kol);
        }
        cout<<navN<<endl;
        dohaNa(redB,kolB)=temp;
    }
    return 0;
}