#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int mat[10005][10000];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>mat[i][j];
        }
    }
    int q;
    cin>>q;
    for(int i=1;i<=q;i++){
        int x,y;
        cin>>x>>y;
        ll s=mat[1][1];
        int j=1;
        int k=1;
        while(j!=n||k!=m){
            if((mat[j+1][k]>=mat[j][k+1]&&(j!=x||k+1!=y))||(mat[j][k+1]>=mat[j+1][k]&&j+1==x&&k==y)){
                ///cout<<"uso sam u prvo"<<endl;
                s+=mat[j][k+1];
                k++;
            }
            else if((mat[j+1][k]<mat[j][k+1]&&(j+1!=x||k!=y))||(mat[j+1][k]>=mat[j][k+1]&&j==x&&k+1==y)){
                ///cout<<"uso sam u drugo"<<endl;
                s+=mat[j+1][k];
                j++;
            }
        }
        s+=mat[n][m];
        cout<<s<<endl;
    }
    return 0;
}