#include <bits/stdc++.h>
using namespace std;

int n,m,matrica[50][50],q,a,b,maks;


int main(){

	cin >> n >> m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >> matrica[i][j];
		}
	}

	cin >> q;
	for(int i=1;i<=q;i++){
		cin >> a >> b;
		if(a==1 and b==2) cout << matrica[1][1]+matrica[2][1]+matrica[2][2] << endl;
		else cout << matrica[1][1]+matrica[1][2]+matrica[2][2] << endl;
	}

return 0;
}