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

int n, m;
const long long INF = 2000000000;
long long f(int x, int y, int bx, int by, vector<vector<long long>>& mapa, vector<vector<long long>>& ans) {
	if (x > m || y > n) return INF;
	if(x == bx && y == by) return INF;
	if (x == m && y == n) return mapa[n][m];

	return min(f(x + 1, y, bx, by, mapa, ans), f(x, y + 1, bx, by, mapa, ans)) + mapa[y][x];
}


int main(int argc, char **argv){
	cin >> n >> m;
	vector<vector<long long>> mapa(n+1, vector<long long>(m+1, 0));
	vector<vector<long long>> ans(n+1, vector<long long>(m+1, 0));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mapa[i][j];
		}
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++){
		int bx, by;
		cin >> by >> bx;
		cout << f(1, 1, bx, by, mapa, ans) << endl;
		//for (int i = 1; i <= n; i++) {
		//	for (int j = 1; j <= m; j++) {
		//		cout << ans[i][j] << " ";
		//	}
		//	cout << endl;
		//}
	}
	return 0;
}