#include <iostream>
#include <vector>
#include <map>

#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)



using namespace std;

vector<vector<int>> navijaci;

int n, m;

long long put(int cx, int cy, int bx, int by) {
	if (cx == bx && cy == by) return INT_MAX;
	if (cx == n - 1 && cy == m - 1) return navijaci[n - 1][m - 1];
	long long minimum = INT_MAX;
	if (cx < n - 1) minimum = min(minimum, put(cx + 1, cy, bx, by));
	if (cy < m - 1) minimum = min(minimum, put(cx, cy + 1, bx, by));
	return minimum + navijaci[cx][cy];
}

long long solve(int x, int y) {
	return put(0, 0, x, y);
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		vector<int> v(m);
		for (int j = 0; j < m; j++) {
			cin >> v[j];
		}
		navijaci.push_back(v);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int x, y;
		cin >> x >> y;
		cout<<solve(x-1, y-1)<<endl;
	}
}