#include <iostream>
#include <vector>
using namespace std;

vector<int> unutrasnji;
vector<vector<int>> traffic;
vector <int> temp;
vector<vector<int>> scenario;


int main()
{
	int m, n;
	cin >> m >> n;

	for (int i = 0; i < m; i++)
	{
		unutrasnji.clear();
		for (int j = 0; j < n; j++)
		{
			int unos;
			cin >> unos;
			unutrasnji.insert(unutrasnji.begin() + j, unos);
		}
		traffic.insert(traffic.begin() + i, unutrasnji);
	}
	
	int q;
	cin >> q;

	for (int i = 0; i < q; i++)
	{
		temp.clear();
		int unos;
		cin >> unos;
		temp.insert(temp.begin(), unos);

		int unos1;
		cin >> unos1;
		temp.insert(temp.begin() + 1, unos1);

		scenario.insert(scenario.begin()+i , temp);
	}

	int result;

	int jStart;
	int jEnd;

	int iBlock;
	int jBlock;
		
	for (int s = 0; s < q; s++)
	{
		vector<int> sc = scenario[s];

		iBlock = sc[0] - 1;
		jBlock = sc[1] - 1;
		result = 0;

		for (int i = 0; i < m; i++)
		{
			if (jBlock == 0)
			{
				if (i == 0) { jStart = 0; }
				else { jStart = n - 1; }
				jEnd = n;
			}

			else {

				if (i < iBlock) {
					if (i == 0) { jStart = 0; }
					else { jStart = jBlock - 1; }
					jEnd = jBlock;
				}

				if (i == iBlock) {
					if (iBlock > 0) { jStart = jBlock - 1; }
					else { jStart = 0; }
					jEnd = jBlock;
				}

				if (i > iBlock) {
					if (i == iBlock + 1) { jStart = jBlock - 1; }
					else { jStart = n - 1; }
					jEnd = n;
				}
			}

			vector<int> line = traffic[i];

			for (int j = jStart; j < jEnd; j++)
			{
				result += line[j];
			}
		}
		cout << result << endl;
	}
}