#include <iostream>
#define ff(i,a,b) for(int (i) = (a); (i) < (b); ++ (i))
using namespace std;
long long a[2000][2000];
int n,m;
void srediti(){
	
    int v,k;
    cin>>v>>k;
    v = v-1;
    k = k-1;
    long long dp[n][m] = {0};
    dp[0][0] = a[0][0];
    dp[v][k] = INT_MAX;
    bool ok = false;
    for(int i = 1;i<n;i++){
    if(i!=v or k!=0){
        dp[i][0] = dp[i-1][0] + a[i][0];
    }
	else ok= true;
    if(ok){
    ff(j,i,n){
    dp[j][0] = INT_MAX;
	}
	i = n-1;
	break;
	}
	}
	ok = false;
	for(int i = 1;i<m;i++){
	if(i!=k or v!=0){
	dp[0][i] = dp[0][i-1] + a[0][i];
    }
	else ok= true;
    if(ok){
    ff(j,i,m){
    dp[0][j] = INT_MAX;
	}
	i = m-1;
	break;
	}
	}
	for(int i = 1;i<n;i++){
        for(int j = 1;j<m;j++){
        if(i!=v or j!=k){
            dp[i][j] = a[i][j] + min(dp[i][j-1],dp[i-1][j]);
            }
        }
    }
    cout<<dp[n-1][m-1]<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    ff(i,0,n){
    ff(j,0,m){
    cin>>a[i][j];
}
}
    int t;
	cin>>t;
    while(t--){
        srediti();
        if(t!=0)
        cout<<endl;
    }
}