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

typedef long long ll;
int n, m;
const ll INF = 2000000000;

const int MAXNM = 1000010;
ll mapa[MAXNM];
ll distFromStart[MAXNM];
ll distFromEnd[MAXNM];

ll findDistFromEnd(int x = 0, int y = 0) {
	if (x >= m || y >= n) return INF;
	int index = y * m + x;
	if (distFromEnd[index] != 0) return distFromEnd[index];
	if (x == m-1 && y == n-1) return mapa[index];

	ll a = findDistFromEnd(x + 1, y);
	ll b = findDistFromEnd(x, y + 1);

	return distFromEnd[index] = min(a, b) + mapa[index];
}

ll findDistFromStart(int x = m-1, int y = n-1) {
	if (x < 0 || y < 0) return INF;
	int index = y * m + x;
	if (distFromStart[index] != 0) return distFromStart[index];
	if (x == 0 && y == 0) return mapa[index];

	ll a = findDistFromStart(x - 1, y);
	ll b = findDistFromStart(x, y - 1);

	return distFromStart[index] = min(a, b) + mapa[index];
}

int main(int argc, char **argv){
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> mapa[i * m + j];
		}
	}
	int q;
	cin >> q;

	//for (int i = 0; i < n; i++) {
	//	for (int j = 0; j < m; j++) {
	//		cout << mapa[i * m + j] << " ";
	//	}
	//	cout << endl;
	//}
//
	findDistFromEnd();
	//for (int i = 0; i < n; i++) {
	//	for (int j = 0; j < m; j++) {
	//		cout << distFromEnd[i * m + j] << " ";
	//	}
	//	cout << endl;
	//}
//
	findDistFromStart();
	//for (int i = 0; i < n; i++) {
	//	for (int j = 0; j < m; j++) {
	//		cout << distFromStart[i * m + j] << " ";
	//	}
	//	cout << endl;
	//}
	
	
	for (int i = 0; i < q; i++){
		int bx, by;
		cin >> by >> bx;
		by--;
		bx--;

		ll ans = INF;
		//cout << "----------" << endl;
		//cout << bx << " " << by << endl;
		if (by < n - 1) {
			by++;
			for (int i = 0; i < bx; i++) {
				int index = by * m + i;
				ll newAns = distFromStart[index] + distFromEnd[index] - mapa[index];
				ans = min(ans, newAns);
				//cout << newAns << " ";
			} 
			by--;
		}

		if (bx < m - 1) {
			bx++;
			for (int i = 0; i < by; i++) {
				int index = i * m + bx;
				ll newAns = distFromStart[index] + distFromEnd[index] - mapa[index];
				ans = min(ans, newAns);
				//cout << newAns << " ";

			}
			bx--;
		}
		//cout << endl;
		cout << ans  << "\n";

	}
	return 0;
}