#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
    int n, m; cin >> n >> m;
    vector<vector<ll>> a(n+1, vector<ll>(m+1));
    vector<vector<ll>> dp(n+1, vector<ll>(m+1, INF));
    vector<vector<ll>> dp_inv(n+2, vector<ll>(m+2, INF));

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (i == 1 && j == 1)
                dp[i][j] = a[i][j];
            else
                dp[i][j] = a[i][j] + min(dp[i-1][j], dp[i][j-1]);
        }
    }
    for (int i = n; i >= 1; i--) {
        for (int j = m; j >= 1; j--) {
            if (i == n && j == m)
                dp_inv[i][j] = a[i][j];
            else {
                dp_inv[i][j] = a[i][j] + min(dp_inv[i+1][j], dp_inv[i][j+1]);
            }
        }
    }
    int q; cin >> q;
    while (q--) {
        int x, y; cin >> x >> y;
        ll ans, put1, put2;
        if (x - 1 >= 1 && y + 1 <= m) {
            int ik = x - 1, jk = y + 1;
            put1 = dp[ik][jk] + dp_inv[ik][jk] - a[ik][jk];
        }
        if (x + 1 <= n && y - 1 >= 1) {
            int ik = x + 1, jk = y - 1;
            put2 = dp[ik][jk] + dp_inv[ik][jk] - a[ik][jk];
        }
        ans = min(put1, put2);

        cout << ans << "\n";
    }
}