#include <bits/stdc++.h>
# define endl            '\n'
# define clr(x,a)        memset(x,a,sizeof(x))
# define vi              vector<int>
# define all(a)          a.begin(), a.end()
# define deb(x)          cout<<#x<<"=="<<x<<endl;
# define pb              push_back
# define sc              scanf
# define pr              printf
# define tc              int t; cin>>t; while(t--)
# define rev(s)          reverse(s.begin(),s.end())
# define linija          cout<<"___________\n";
# define sza(x)          (int)x.size()
using namespace std;
typedef long long ll;
const int mxN=200005, mxM=200005, mod=1e9+7, LOG=30;
const ll inf=50000000000000000;
template<typename T> T nzd(T a, T b){if(b==0) return a;else return nzd(b, a%b);}
template<typename T> T nzs(T a, T b){return(a*(b/nzd(a,b)));}
template<typename T> T stepenuj(T e, T n){T x=1,p=e;while(n){if(n&1)x=(x*p)%mod;p=(p*p)%mod;n>>=1;}return x;}
template<typename T> inline T na2(T x){return x*x;}
int n, m, x, y;

vector<ll> dp[mxN];
vi mat[mxN];

bool u_granici(int r, int c){
   return (r>=1 && c>=1 && r<=n && c<=m && (r!=x || c!=y));
}
void solve(){
   ll tren1, tren2;
   dp[1][1]=mat[1][1];
   for(int i=1; i<=n; i++){
      for(int j=1; j<=m; j++){
         if(i==1 && j==1) continue;
         if(i==x && j==y) continue;
         tren1=tren2=inf;
         if(u_granici(i, j-1)) tren1=dp[i][j-1];
         if(u_granici(i-1, j)) tren2=dp[i-1][j];
         dp[i][j]=min(tren1, tren2)+mat[i][j];
      }
   }
}
void prep(){
   for(int i=1; i<=n; i++){
      for(int j=1; j<=m; j++) dp[i].pb(j);
   }
}
int main(){
   //freopen("in.txt", "r", stdin);
   cin>>n>>m;
   for(int i=1; i<=n; i++){
      mat[i].pb(-1);
      dp[i].pb(-1);
   }
   for(int i=1; i<=n; i++){
      for(int j=1; j<=m; j++){
         sc("%d", &x);
         mat[i].pb(x);
      }
   }
   prep();
   tc{
      sc("%d%d", &x, &y);
      solve();
      pr("%lld\n", dp[n][m]);
   }
	return 0;
}