#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
unsigned long long n, m, p, q;
unsigned long long maks = 9223372036854775807;

vector<vector<unsigned long long>> minzbir(const vector<vector<unsigned long long>>& M) {
  
  vector<vector<unsigned long long>> dp(n);
  for (int i = 0; i < n; i++)
    dp[i].resize(m);
  dp[0][0] = M[0][0];
  for (int v = 1; v < n; v++)
    dp[v][0] = dp[v-1][0] + M[v][0];
  for (int k = 1; k < m; k++)
    dp[0][k] = dp[0][k-1] + M[0][k];
  for (int v = 1; v < n; v++)
    for (int k = 1; k < m; k++)
      dp[v][k] = min(dp[v-1][k] + M[v][k], dp[v][k-1] + M[v][k]);
  return dp;
}





int main() {
    ios_base::sync_with_stdio(false);  
cin.tie(NULL);
  cin >> n>>m;
  vector<vector<unsigned long long>> M(n);

  for (long long i = 0; i < n; i++) {
    M[i].resize(m);
    for (long long j = 0; j < m; j++)
      cin >> M[i][j];
  }
  
    long long z;
    cin>>z;
    for(long long i=0; i<z; i++ ){
        int t, e, p;
        cin>>p>>e;
        p-=1;
        e-=1;
        t = M[p][e];
        M[p][e] = maks;
        vector<vector<unsigned long long>> dp = minzbir(M);
    
        cout << dp[n-1][m-1] << endl;
        M[p][e] = t;
        
    }
  return 0;
}