#include <bits/stdc++.h>
using namespace std;
int m,n,q,a,b;
long long navijaci[2005][2005];
long long dp[2005][2005];
int minZbir(int x,int y){
long long  stari=navijaci[x][y];
  memcpy(dp,navijaci,sizeof(dp));

  navijaci[x][y]=1e18;
  for(int i=0;i<=n+1;i++){
    dp[i][0]=1e18;
  }
  for(int i=0;i<=m+1;i++){
    dp[0][i]=1e18;
  }
  dp[0][1]=0;
  for (int v=1;v<=n;v++){
    for (int k=1;k<=m;k++){
      dp[v][k] = min(dp[v-1][k] + navijaci[v][k], dp[v][k-1] + navijaci[v][k]);

    }
  }
  navijaci[x][y]=stari;
  return dp[n][m];

}
int main() {
  cin>>n>>m;
  for(int i=1;i<=n;i++) {
    for (int j=1;j<=m;j++){
      cin>>navijaci[i][j];
      }
  }
  cin>>q;
  int x1=0,y1=0;
  long long stari=0;

  while(q--){
    cin>>a>>b;

    cout<<minZbir(a,b)<<endl;
}
  return 0;
}