#include <iostream>
#include<vector>

using namespace std;

int main()
{
	vector<int> a;
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			int br;
			cin >> br;
			a.push_back(br);
		}
	}
	vector<pair<int, int>> q;
	int Q;
	cin >> Q;
	for (int i = 0; i < Q; i++)
	{
		int x, y;
		cin >> x >> y;
		q.push_back(make_pair(x-1, y-1));
	}
	for (int k = 0; k < Q; k++) {
		int i = 0, j = 0;
		int navijaci = a[0];
		if (q[k].first < n - 1 && q[k].second < m - 1) {
			while (i < n - 1 || j < m - 1) {
				if (i + 1 == q[k].first && j == q[k].second)
					j++;
				else if ((i == q[k].first && j + 1 == q[k].second))
					i++;
				else if (i == n - 1)
					j++;
				else if (j == m - 1)
					i++;
				else
				{
					if (a[i * m + j + 1] <= a[(i + 1) * m + j])
						j++;
					else if (a[i * m + j + 1] > a[(i + 1) * m + j])
						i++;
				}
				navijaci += a[i * m + j];
			}
		}
		else if (q[k].first == n - 1) {
			while (i < n - 1 || j < m - 1) {
				if (i + 1 == q[k].first && j == q[k].second)
					j++;
				else if ((i == q[k].first && j + 1 == q[k].second))
					i++;
				else if (i == n - 1)
					j++;
				else if (j == m - 1)
					i++;
				else
				{
					if (a[i * m + j + 1] <= a[(i + 1) * m + j] && i<q[k].first-1)
						j++;
					else if (a[i * m + j + 1] > a[(i + 1) * m + j])
						i++;
					else if (j <= q[k].second)
						j++;
				}
				navijaci += a[i * m + j];
			}
		}
		else if (q[k].second == m - 1) {
			while (i < n - 1 || j < m - 1) {
				if (i + 1 == q[k].first && j == q[k].second)
					j++;
				else if ((i == q[k].first && j + 1 == q[k].second))
					i++;
				else if (i == n - 1)
					j++;
				else if (j == m - 1)
					i++;
				else
				{
					if (a[i * m + j + 1] <= a[(i + 1) * m + j] && j < q[k].second-1)
						j++;
					else if (a[i * m + j + 1] > a[(i + 1) * m + j])
						i++;
					else if (i <= q[k].first)
						i++;
				}
				navijaci += a[i * m + j];
			}
		}
		cout << navijaci << endl;
	}
	return 0;
}