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

using namespace std;
int n, m;


vector<vector<int>> minzbir(const vector<vector<int>>& M) {
  
  vector<vector<int>> 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() {
  cin >> n>>m;
  vector<vector<int>> M(n);
  
  for (int i = 0; i < n; i++) {
    M[i].resize(m);
    for (int j = 0; j < m; j++)
      cin >> M[i][j];
  }
  
    int q;
    cin>>q;
    for(int i=0; i<q; i++ ){
        int t, e, p;
        cin>>p>>e;
        p-=1;
        e-=1;
        t = M[p][e];
        M[p][e] = 1000000001;
        vector<vector<int>> dp = minzbir(M);
    
        cout << dp[n-1][m-1] << endl;
        M[p][e] = t;
    }
  return 0;
}