#include <bits/stdc++.h>
using namespace std;
int n,m,matrica[2005][2005],q,a,b;
unsigned long long sol;
void pronadjiPut(int x, int y, unsigned long long cena){
  //cout << "X: " << x << " Y: " << y << endl;
  if(x==n and y==m){
    sol=min(sol,cena);
  }
  if(matrica[x+1][y]>0){
    pronadjiPut(x+1,y,cena+matrica[x+1][y]);
  }
  if(matrica[x][y+1]>0){
    pronadjiPut(x,y+1,cena+matrica[x][y+1]);
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=1; i<=n; i++){
    for(int j=1; j<=m; j++){
      cin >> matrica[i][j];
    }
  }
  cin >> q;
  if(q<=2000){
  while(q--){
    sol=1e18;
    scanf("%d%d",&a,&b);
    int temp=matrica[a][b];
    matrica[a][b]=-1;
    pronadjiPut(1,1,matrica[1][1]);
    printf("%llu\n",sol);
    matrica[a][b]=temp;
  }
  }
  return 0;
}