#include <iostream>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(0);
	int n, m, q;
	cin >> n >> m;
	int navijaci[n][m], cpy = n;
	while(cpy > 0)
	{
		int j = n - cpy;
		for (int i = 0; i < m; i++)
		{
			cin >> navijaci[j][i];
		}
		cpy--;
	}
	cin >> q;
	int zb = navijaci[0][0];
	for (int i = 0; i < q; i++)
	{
		int x1, y1;
		cin >> x1 >> y1;
		x1--, y1--;
		int gkeep = navijaci[x1][y1];
		navijaci[x1][y1] = 2147483647;
		int x = 0, y = 0;
		while (x < n - 1 && y < m - 1)
		{
			if (navijaci[x + 1][y] < navijaci[x][y + 1])
			{
				zb += navijaci[x + 1][y];
				x++;
			} else
			{
				zb += navijaci[x][y + 1];
				y++;
			}
		}
		if (x == n)
		{
			while(y < m - 1)
			{
				if (navijaci[x][y + 1] != 2147483647)
				{
					zb += navijaci[x][y + 1];
				}
				y++;
			}
		} else
		{
			while(x < n - 1)
			{
				if (navijaci[x + 1][y] != 2147483647)
				{
					zb += navijaci[x + 1][y];
				}
				x++;
			}
		}
		cout << zb << endl;
		zb = navijaci[0][0];
		navijaci[x1][y1] = gkeep;
	}
}