{
ios_base::sync_with_stdio(false); cin.tie(NULL);

int n, m, q;
cin>>n>>m;

vector<vector<int>> a(n+1, vector<int>(m+1)), dp(n+1,vector<int>(m+1)), dpt(n+1,vector<int>(m+1));
vector<vector<char>> v(n+1, vector<char>(m+1,0)); //1 postoji put, 0 ne postoji
for(int i = 1; i<=n; i++){
	for(int j = 1; j<=m;j++){
		cin>>a[i][j];
	}
}	
dp[1][1] = a[1][1];

for(int i = 1; i<=n; i++){
	for(int j = 1; j<=m;j++){
		if(i==1 && j==1) continue;
		if(j==1){
			dp[i][j] = dp[i-1][j] + a[i][j];
		}
		else if(i==1){
			dp[i][j] = dp[i][j-1] + a[i][j];
		}
		else{
			dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + a[i][j];
		}
	}
}
//printVectorMatrix(dp);

//finding the path from end to start
queue<pair<int,int>> qu;
qu.push(make_pair(n,m));

while(!qu.empty()){
	pair<int,int> curr = qu.front();
	int i = curr.F, j = curr.S;
	qu.pop();
	if(i == 1 && j == 1) continue;

	if(i==1){
		qu.push(make_pair(i,j-1));
		v[i][j-1] = 1;
	}
	else if(j==1){
		qu.push(make_pair(i-1,j));
		v[i-1][j] = 1;
	}
	else{
		if(dp[i-1][j] == dp[i][j-1]){
			v[i-1][j] = 1;
			v[i][j-1] = 1;
			qu.push(make_pair(i-1,j));
			qu.push(make_pair(i,j-1));
		}
		else{
			int t1 = dp[i-1][j], t2 = dp[i][j-1];
			if(t1<t2){
				qu.push(make_pair(i-1,j));
				v[i-1][j] = 1;
			}
			else{
				qu.push(make_pair(i,j-1));
				v[i][j-1] = 1;
			}
		}
	}
	
}
//printVectorMatrixC(v);
cin>>q;
while(q--){
	int x,y;
	cin>>x>>y;

	if(v[x][y]==0) cout<<dp[n][m]<<endl;		
	else{
		dpt[x][y] = INT_MAX;

		for(int i = x; i<=n; i++){
			for(int j = y; j<=m;j++){
				if(i==x && j==y) continue;
				if(j==1){
					dpt[i][j] = INT_MAX;
				}
				else if(i==1){
					dpt[i][j] = INT_MAX;
				}
				else if(j==y){
					dpt[i][j] = min(dpt[i-1][j], dp[i][j-1]) + a[i][j];
				}
				else if(i==x){
					dpt[i][j] = min(dpt[i][j-1], dp[i-1][j]) + a[i][j];
				}
				else{
					dpt[i][j] = min(dpt[i-1][j], dpt[i][j-1]) + a[i][j];
				}
			}
		}
		cout<<dpt[n][m]<<endl;
		//printVectorMatrix(dpt);
	}
}


return 0;
}