#include <iostream>

using namespace std;

int f(int x, int y, int bx, int by, int n, int m, int a[][10000], int c){
	if ((x == n) and (y == m)){
		return c;
	};
	int k, l;
	if ((not((x+1 == bx) and (y == by))) and (x+1 <= n)) k = f(x+1, y, bx, by, n, m, a, c+a[x+1][y]);
	else if (((x+1 == bx) and (y == by)) and (y+1 > m)) return 2147483647;
	else{
		l = f(x, y+1, bx, by, n, m, a, c+a[x][y+1]);
		return l;
	}
	if ((not((x == bx) and (y+1 == by))) and (y+1 <= m)) l = f(x, y+1, bx, by, n, m, a, c+a[x][y+1]);
	else if (((x+1 == bx) and (y == by)) and (y+1 > m)) return 2147483647;
	else return k;
	if (k < l) return k;
	else return l;
}

int main(){
	int n, m;
	cin >> n >> m;
	int a[n][10000];
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cin >> a[i][j];
		}
	}
	int Q;
	cin >> Q;
	int q[Q][2];
	for (int i = 0; i < Q; i++){
		cin >> q[i][0] >> q[i][1];
	}
	for (int i = 0; i < Q; i++){
		cout << f(0, 0, q[i][0]-1, q[i][1]-1, n-1, m-1, a, a[0][0]) << endl;
	}
}