#include <bits/stdc++.h>
#include <type_traits>

using namespace std;

typedef long long ll;
const double EPS = 1E-6;
const ll MOD = 1000000007;
const ll HIGH = 4611686018427387904; // 2^62

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    ll a[n][m];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> a[i][j];
        }
    }
    int Q; cin >> Q;
    for (int qwers = 0; qwers < Q; ++qwers) {
        int x, y;
        cin >> x >> y;
        vector<vector<ll>> dp(n+1,  vector<ll>(m+1));
        dp[0][0] = a[0][0];
        int z = a[x-1][y-1];
        a[x-1][y-1] = HIGH;
        for (int i = 1; i < n; ++i) dp[i][0] = dp[i-1][0] + a[i][0];
        for (int i = 1; i < m; ++i) dp[0][i] = dp[0][i-1] + a[0][i];
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j < m; ++j) {
                if (dp[i][j] != HIGH) {
                    dp[i][j] = a[i][j] + min(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        cout << dp[n-1][m-1] << '\n';
        a[x-1][y-1] = z;
    }
    return 0;
}
/*
    Overflow?
    n = 1, n = 0, n = max
    array bound?
    a*b/c != a*(b/c) !!!
*/