#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned u;

#define INF INT_MAX
#define MOD (1000000007)
#define PI arccos(-1)

#define pll pair<long long, long long>
#define mp make_pair

#define vll vector<ll>
#define vb vector<bool>
#define vs vector<string>
#define vp vector<pll>
#define pb push_back
#define all(v) v.begin(), v.end()
#define print_vect(v) for (auto it : v) cout << it << " ";

#define pq priority_queue<ll>
#define pqg priority_queue<ll, vll, greater<ll>>
#define set set<ll>
#define mset multiset<ll>
#define queue queue<ll>

ll add(ll a, ll b) { a += b; return a >= MOD ? a - MOD : a; }
ll sub(ll a, ll b) { a -= b; return a < 0 ? a + MOD : a; }
ll mul(ll a, ll b) { return (long long)(a) * (long long)(b) % MOD; }
 
void sadd(ll &a, ll b) { a = add(a, b); }
void ssub(ll &a, ll b) { a = sub(a, b); }
void smul(ll &a, ll b) { a = mul(a, b); }

ll nzs(ll a, ll b){ 
    return (a / __gcd(a, b)) * b; 
}

const ll N = 1e9 + 5;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, m;
    cin >> n >> m;
    ll mat[n][m];
    ll dp[n][m];
    bool putanja[n][m] = {false};
    putanja[0][0] = putanja[n - 1][m - 1] = true;
    for(ll i = 0 ; i < n ; i++){
        for(ll j = 0 ; j < m ; j++){
            cin >> mat[i][j];
            dp[i][j] = mat[i][j];
        }
    }
    for(ll i = 1 ; i < n ; i++){
        dp[i][0] += dp[i - 1][0];
    }
    for(ll i = 1 ; i < m ; i++){
        dp[0][i] += dp[0][i - 1];
    }
    for(ll i = 1 ; i < n ; i++){
        for(ll j = 1 ; j < m ; j++){
            dp[i][j] += min(dp[i][j - 1], dp[i - 1][j]);
        }
    }
    
    ll pi = n - 1;
    ll pj = m - 1;
    while(pi > 0 || pj > 0){
        if(dp[pi - 1][pj] == dp[pi][pj] - mat[pi][pj]){
            putanja[pi - 1][pj] = true;
            pi--;
        }
        else{
            putanja[pi][pj - 1] = true;
            pj--;
        }
    }

    ll q;
    cin >> q;
    for(ll z = 0 ; z < q ; z++){
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        ll ans = -1;
        if(!putanja[a][b]){
            ans = dp[n - 1][m - 1];
        }
        else{
            ll dp1[n][m];
            for(ll i = 0 ; i < n ; i++){
                for(ll j = 0 ; j < m ; j++){
                    dp1[i][j] = mat[i][j];
                }
            }
            dp1[a][b] = INF;
            for(ll i = 1 ; i < n ; i++){
                dp1[i][0] += dp1[i - 1][0];
            }
            for(ll i = 1 ; i < m ; i++){
                dp1[0][i] += dp1[0][i - 1];
            }
            for(ll i = 1 ; i < n ; i++){
                for(ll j = 1 ; j < m ; j++){
                    dp1[i][j] += min(dp1[i][j - 1], dp1[i - 1][j]);
                }
            }
            ans = dp1[n - 1][m - 1];
        }
        cout << ans << "\n";
    }
    return 0;
}