#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pii;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
 
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
	
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define vt vector

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x);
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

const ll LINF = 1e16;
const int INF = 1e9, MOD = 1e9+7, mod = 998244353;
const char nl = '\n', sp=' ';
const int mxN = 2e5+10;


void solve() {
    int n,m; cin>>n>>m;
    ll A[n][m],up[n][m],dn[n][m];
    FOR(i,n) FOR(j,m) cin>>A[i][j];
    up[0][0] = A[0][0];
    FOR(i,1,n) up[i][0] = A[i][0] + up[i-1][0];
    FOR(i,1,m) up[0][i] = A[0][i] + up[0][i-1];
    FOR(i,1,n) FOR(j,1,m) up[i][j] = A[i][j] + min(up[i-1][j],up[i][j-1]);

    for(int i = n-1; i>=0; --i)
        for(int j = m-1; j>=0; --j){
            dn[i][j] = A[i][j];
            if(i==n-1 && j==m-1) continue;
            ll mn = LINF;
            if(i < n-1) ckmin(mn,dn[i+1][j]);
            if(j < m-1) ckmin(mn,dn[i][j+1]);
            dn[i][j] += mn;
        }

    ll ans[n][m];
    ll tmp;
    memset(ans, 0x3f, sizeof ans);
    for(int i = 0; i < n-1; ++i){
        ll mn = LINF;
        for(int j = 0; j < m; ++j){
            ckmin(ans[i][j],mn);
            tmp = up[i][j] + dn[i+1][j];
//            if(i > 0) tmp += up[i-1][j];
            ckmin(mn,tmp);
        }
    }

    for(int i = n-1; i; --i){
        ll mn = LINF;
        for(int j = m-1; j>=0; --j){
            ckmin(ans[i][j],mn);
            tmp = dn[i][j] + up[i-1][j];
            ckmin(mn,tmp);
        }
    }

    /*
    mn = LINF;
    for(int j = m-1; j>=0; --j){
        ckmin(ans[i][j],mn);
        tmp = A[i][j];
        if(i < n-1) tmp += dn[i+1][j];
        if(i > 0) tmp += up[i-1][j];
        ckmin(mn,tmp);
    }
    */

    int q; cin>>q;
    while(q--){
        int x,y; cin>>x>>y;
        x--; y--;
        cout << ans[x][y] << nl;
    }
}
 
int main() {
    cin.tie(0)->sync_with_stdio(0); 
    cin.exceptions(cin.failbit);

    int T = 1;
//    cin >> T;
    while(T--) {
        solve();
    }

	return 0;
}