#include <iostream>

using namespace std;

int main()
{
   unsigned long long int n,m;
   cin>>n>>m;
   int niz[n][m];
   for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        cin>>niz[i][j];
    }
   }
   m=m-1;
   n=n-1;
   int q;
   cin>>q;
   int x,y;
   for(int l=0;l<q;l++){
    cin>>x>>y;
    x=x-1;
    y=y-1;
    int zbir=0,i=0,j=0;
    zbir=zbir+niz[i][j];
    while(n!=i || m!=j){
        if(i+1==x && j+1==y){
            zbir=zbir+niz[i+1][j];
            i++;
        }else if(i==x && j+1==y){
            zbir=zbir+niz[i+1][j];
            i++;
        }else if(i+1==x && j==y){

            zbir=zbir+niz[i][j+1];
            j++;
        }else if(i==n){

            zbir=zbir+niz[i][j+1];
            j++;
        }else if(j==m){

            zbir=zbir+niz[i+1][j];
            i++;
        }else if(niz[i+1][j]>niz[i][j+1]){

            zbir=zbir+niz[i][j+1];
            j++;
        }else if(niz[i][j+1]>niz[i+1][j]){

            zbir=zbir+niz[i+1][j];
            i++;
        }

    }
cout<<zbir<<endl;
   }
   return 0;
}