#include <bits/stdc++.h>
#define ll long long
using namespace std;
long long najmSuma(int i, int j, vector<vector<int>> a, vector<vector<long long>> dp){
    if(i==0 && j==0)
        return a[i][j];
    if(i<0 || j<0)
        return 2e9;
    if(dp[i][j] != -1)
        return dp[i][j];
    long long gore = a[i][j] + najmSuma(i-1,j,a,dp);
    long long levo = a[i][j] + najmSuma(i,j-1,a,dp);
    return dp[i][j] = min(gore,levo);
}
void resi(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>> a(n,vector<int> (m));
    vector<vector<long long>> dp(n,vector<long long>(m,-1));
    for(int i=0;i<n;++i){
        for(int j = 0;j<m;++j){
            cin>>a[i][j];
        }
    }
    int q,x,y;
    cin >> q;
    for(int i=0;i<q;++i){
        cin >> x >> y;
        long long temp = a[x-1][y-1];
        a[x-1][y-1] = 2e9;
        cout << najmSuma(n-1,m-1,a,dp) << '\n';
        a[x-1][y-1] = temp;
    }

}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
    resi();e
    return 0;
}