#include<bits/stdc++.h>
using namespace std;
int n,m,q,xb,yb,x,y,temp,navN;
int* doha;
int& dohaNa(int x,int y){
    x--;
    y--;
    static int tmp;
    if(x>=n||y>=m)
        return tmp=INT_MAX;
    return doha[x+y*n];
}
int main(){
    cin>>n>>m;
    doha=new int[n*(long long)m];
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            cin>>dohaNa(j,i);
    cin>>q;
    while(q--){
        cin>>xb>>yb;
        temp=dohaNa(xb,yb);
        dohaNa(xb,yb)=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(xb,yb)=temp;
    }
    return 0;
}