#include<bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    long long a,b;
    cin >> a >> b;
    long long q[a][b];
    for(long long i = 0; i < a; i++){
        for(long long j = 0; j < b; j++){
            cin >> q[i][j];
        }
    }
    long long t;
    cin >> t;
    for(long long i = 0; i < t; i++){
        long long x,y;
        cin >> x >> y;
        long long r = q[x-1][y-1];
        q[x-1][y-1] = 2147483648;
        long long dp[a][b];
        dp[0][0] = q[0][0];
        for(long long i = 1; i < a; i++){
            dp[i][0] = q[i][0] + dp[i-1][0];
        }
        for(long long i = 1; i < b; i++){
            dp[0][i] = q[0][i] + dp[0][i-1];
        }
        for(long long i = 1; i < a; i++){
            for(long long j = 1; j < b; j++){
                dp[i][j] = q[i][j] + min(dp[i-1][j], dp[i][j-1]);
            }
        }
        cout << dp[a-1][b-1] << "\n";
        q[x-1][y-1] = r;
    }
}