#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

bool da_li_postoji(ll i,ll j,ll n,ll m){
    if(i<0||j<0||i>=n||j>=m){
        return false;
    }
    else{
        return true;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    vector<vector<ll>> mat(n,vector<ll>(m)),mat1(n,vector<ll>(m)),mat2(n,vector<ll>(m));
    for(ll i=0;i<n;i++){
        for(ll j=0;j<m;j++){
            cin>>mat[i][j];
        }
    }
    ll q;
    cin>>q;
    vector<pair<ll,ll>> Q(q);
    for(ll i=0;i<q;i++){
        cin>>Q[i].first>>Q[i].second;
        Q[i].first--;Q[i].second--;
    }
    for(ll i=0;i<n;i++){
        for(ll j=0;j<m;j++){
            if(da_li_postoji(i-1,j,n,m)&&da_li_postoji(i,j-1,n,m)){
                if(mat[i-1][j]<=mat[i][j-1]){
                    mat1[i][j]=mat1[i-1][j]+mat[i][j];
                }
                else{
                    mat1[i][j]=mat1[i][j-1]+mat[i][j];
                }
            }
            else if(!da_li_postoji(i-1,j,n,m)&&da_li_postoji(i,j-1,n,m)){
                mat1[i][j]=mat1[i][j-1]+mat[i][j];
            }
            else if(da_li_postoji(i-1,j,n,m)&&!da_li_postoji(i,j-1,n,m)){
                mat1[i][j]=mat1[i-1][j]+mat[i][j];
            }
            else{
                mat1[i][j]=mat[i][j];
            }
        }
    }
    for(ll i=n-1;i>=0;i--){
        for(ll j=m-1;j>=0;j--){
            if(da_li_postoji(i+1,j,n,m)&&da_li_postoji(i,j+1,n,m)){
                if(mat[i+1][j]<=mat[i][j+1]){
                    mat2[i][j]=mat2[i+1][j]+mat[i][j];
                }
                else{
                    mat2[i][j]=mat2[i][j+1]+mat[i][j];
                }
            }
            else if(!da_li_postoji(i+1,j,n,m)&&da_li_postoji(i,j+1,n,m)){
                mat2[i][j]=mat2[i][j+1]+mat[i][j];
            }
            else if(da_li_postoji(i+1,j,n,m)&&!da_li_postoji(i,j+1,n,m)){
                mat2[i][j]=mat2[i+1][j]+mat[i][j];
            }
            else{
                mat2[i][j]=mat[i][j];
            }
        }
    }
    ll t1,t2;
    for(ll i=0;i<q;i++){
        if(da_li_postoji(Q[i].first-1,Q[i].second+1,n,m)&&da_li_postoji(Q[i].first+1,Q[i].second-1,n,m)){
            t1=mat1[Q[i].first-1][Q[i].second+1]+mat2[Q[i].first-1][Q[i].second+1]-+mat[Q[i].first-1][Q[i].second+1];
            t2=mat1[Q[i].first+1][Q[i].second-1]+mat2[Q[i].first+1][Q[i].second-1]-+mat[Q[i].first+1][Q[i].second-1];
            if(t1<=t2){
                cout<<t1<<'\n';
            }
            else{
                cout<<t2<<'\n';
            }
        }
        else if(!da_li_postoji(Q[i].first-1,Q[i].second+1,n,m)&&da_li_postoji(Q[i].first+1,Q[i].second-1,n,m)){
            cout<<mat1[Q[i].first+1][Q[i].second-1]+mat2[Q[i].first+1][Q[i].second-1]-+mat[Q[i].first+1][Q[i].second-1]<<'\n';
        }
        else if(da_li_postoji(Q[i].first-1,Q[i].second+1,n,m)&&!da_li_postoji(Q[i].first+1,Q[i].second-1,n,m)){
            cout<<mat1[Q[i].first-1][Q[i].second+1]+mat2[Q[i].first-1][Q[i].second+1]-+mat[Q[i].first-1][Q[i].second+1]<<'\n';
        }
    }
    return 0;
}