#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int INF=1e9;
const ll LINF=1e18,MOD=998244353;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()s
#define endl "\n"
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define pb push_back
#define PRINTvec(niz) { cerr<<#niz<<"="; for(auto _i:niz) cerr<<_i<<" "; cerr<<endl; }
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

void Md(ll& x){
    if(x>=MOD) x%=MOD;
}

void Solve(){
    int N,M; cin>>N>>M;
    vector<vector<ll>> a(N+1,vector<ll>(M+1,0));
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            cin>>a[i][j];
        }
    }
    vector<vector<ll>> dp1(N+2,vector<ll>(M+2,LINF));
    vector<vector<ll>> dp2(N+2,vector<ll>(M+2,LINF));
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            if(i==1 && j==1){
                dp1[i][j]=a[i][j];
                continue;
            }
            dp1[i][j]=min(dp1[i-1][j]+a[i][j],dp1[i][j-1]+a[i][j]);
        }
    }
    for(int i=N;i>=1;i--){
        for(int j=M;j>=1;j--){
            if(i==N && j==M){
                dp2[i][j]=a[i][j];
                continue;
            }
            dp2[i][j]=min(dp2[i+1][j]+a[i][j],dp2[i][j+1]+a[i][j]);
        }
    }
    vector<vector<ll>> mi(N+2,vector<ll>(M+2,LINF));
    vector<vector<ll>> mj(M+2,vector<ll>(N+2,LINF));
    for(int i=1;i<=N;i++){
        for(int j=M;j>=1;j--){
            mi[i][j]=min(mi[i][j+1],dp1[i][j]+dp2[i][j]-a[i][j]);
        }
    }
    for(int j=1;j<=M;j++){
        for(int i=N;i>=1;i--){
            mj[j][i]=min(mj[j][i+1],dp1[i][j]+dp2[i][j]-a[i][j]);
        }
    }
    int Q; cin>>Q;
    while(Q--){
        int i,j; cin>>i>>j;
        cout<<min(mi[i-1][j+1],mj[j-1][i+1])<<endl;
    }

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t; t=1;
    //cin>>t;;
    while(t--){
        Solve();
    }
    return 0;
}