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

long long a[1000000];
long long b[1000000];
long long x[1000000];
bool c[1000000];
bool cx[1000000];
bool d[1000000];
bool dx[1000000];
int q[1000000];

void ajeb(long long a[], long long b[], int n, int m)
{
	for (size_t i = 0; i < n; i++)
	{
		for (size_t j = 0; j < m; j++)
		{
			b[i * m + j] = a[i * m + j];
		}
	}
}

void cjec(bool a[], bool b[], int n, int m)
{
	for (size_t i = 0; i < n; i++)
	{
		for (size_t j = 0; j < m; j++)
		{
			b[i * m + j] = a[i * m + j];
		}
	}
}


/*void checkSides(long long x[], long long a[], bool c[], int xq, int yq, int m)
{
	if (cx[xq * m + yq + 1])
	{
		x[xq * m + yq + 1] = a[xq * m + yq + 1];
		if (min(x[xq * m + yq + 1 - m], x[xq * m + yq]) == x[xq * m + yq + 1 - m])
		{
			
			x[xq * m + yq + 1] += x[xq * m + yq + 1-m];
			cx[xq * m + yq + 1] = false;
			cout << "4";
			checkSides(x, a, c, xq, yq + 1, m);
			checkSides(x, a, c, xq + 1, yq, m);
			cout << "4";
		}
		else
		{
			
			x[xq * m + yq + 1] += x[xq * m + yq];
			cx[xq * m + yq + 1] = true;
			cout << "4";
			checkSides(x, a, c, xq, yq + 1, m);
			checkSides(x, a, c, xq + 1, yq, m);
			cout << "4";
		}
	}
	if (!cx[xq * m + m + yq])
	{
		x[xq * m + yq + m] = a[xq * m + yq + m];
		if (min(x[xq * m + yq], x[xq * m + yq - 1 + m]) == x[xq * m + yq])
		{

			x[xq * m + yq + m] += x[xq * m + yq];
			cx[xq * m + yq + m] = false;
			cout << "4";
			checkSides(x, a, c, xq, yq + 1, m);
			checkSides(x, a, c, xq + 1, yq, m);
			cout << "4";
		}
		else
		{

			x[xq * m + yq + m] += x[xq * m + yq + m - 1];
			cx[xq * m + yq + m] = true;
			cout << "4";
			checkSides(x, a, c, xq, yq + 1, m);
			checkSides(x, a, c, xq + 1, yq, m);
			cout << "4";
		}
	}
}*/

int main()
{
	int n, m;
	cin >>n >> m;
	for (size_t i = 0; i < n; i++)
	{
		for (size_t j = 0; j < m; j++)
		{
			cin >> a[i * m + j];
		}
	}
	
	

	int qN;
	cin >> qN;
	for (size_t i = 0; i < qN; i++)
	{
		cin >> q[i * 2 + 1] >> q[i * 2];
		q[i * 2] -= 1;
		q[i * 2 + 1] -= 1;
	}


	ajeb(a, b, n, m);


	for (size_t i = 1; i < n; i++)
	{
		b[i * m] += b[(i - 1) * m];
	}
	for (size_t i = 1; i < m; i++)
	{
		b[i] += b[i-1];
	}

	for (size_t i = 0; i < m; i++)
	{
		c[i] = true;
	}

	for (size_t i = 1; i < n; i++)
	{
		for (size_t j = 1; j < m; j++)
		{
			if (min(b[(i - 1) * m + j], b[i * m + j - 1]) == b[(i - 1) * m + j])
			{
				b[i * m + j] += b[(i - 1) * m + j];
				c[i * m + j] = false;
			}
			else 
			{
				b[i * m + j] += b[i * m + j - 1];
				c[i * m + j] = true;
				
			}
		}
	}

	//test
	int permn = n - 1;
	int permm = m - 1;
	d[n*m-1] = true;

	for (size_t i = 0; i < n+m-1; i++)
	{
		if (c[permn * m + permm])
		{
			d[permn * m + permm - 1] = true;
			permm -= 1;
		}
		else
		{
			d[(permn-1) * m + permm] = true;
			permn -= 1;
		}
	}
	


	for (size_t k = 0; k < qN; k++)
	{
		ajeb(b, x, n, m);
		cjec(c, cx, n, m);
		for (size_t i = 0; i < n*m-1; i++)
		{
			dx[i] = false;
		}

		if (!d[q[k * 2 + 1] * m + q[k * 2]])
		{
			cout << b[m * n - 1] << "\n";
		}
		else 
		{
			if (q[k * 2 + 1] == 0)
			{
				x[q[k * 2 + 1] * m + q[k * 2]] = INT32_MAX;
				
				for (size_t i = q[k * 2]; i < m; i++)
				{
					x[m + i] = a[m + i];
					x[m + i] += x[m + i - 1];
				}

				for (size_t i = 2; i < n; i++)
				{
					for (size_t j = q[k*2]; j < m; j++)
					{
						
						if (min(x[(i - 1) * m + j], x[i * m + j - 1]) == x[(i - 1) * m + j])
						{
							x[m * i + j] = a[m * i + j];
							x[i * m + j] += x[(i - 1) * m + j];
							cx[i * m + j] = false;
						}
						else
						{
							x[m * i + j] = a[m * i + j];
							x[i * m + j] += x[i * m + j - 1];
							cx[i * m + j] = true;
						}
					}
				}

				cout << x[m * n - 1] << "\n";
				/*
				//test
				permn = n - 1;
				permm = m - 1;
				dx[n * m - 1] = true;

				for (size_t i = 0; i < n + m - 1; i++)
				{
					if (cx[permn * m + permm])
					{
						dx[permn * m + permm - 1] = true;
						permm -= 1;
					}
					else
					{
						dx[(permn - 1) * m + permm] = true;
						permn -= 1;
					}
				}
				for (size_t i = 0; i < n; i++)
				{
					for (size_t j = 0; j < m; j++)
					{
						if (dx[i * m + j]) cout << "1";
						else cout << "0";
					}
					cout << "\n";
				}
				for (size_t i = 0; i < n; i++)
				{
					for (size_t j = 0; j < m; j++)
					{
						cout << x[i * m + j] << " ";
					}
					cout << "\n";
				}*/

			}
			else if (q[k * 2] == 0)
			{
				x[q[k * 2 + 1] * m + q[k * 2]] = INT32_MAX;

				
				for (size_t i = q[k * 2 + 1]; i < n; i++)
				{
					x[m * i + 1] = a[m * i + 1];
					x[m * i + 1] += x[m * i - m + 1];
				}

				for (size_t i = q[k*2+1]; i < n; i++)
				{
					for (size_t j = 2; j < m; j++)
					{
						
						if (min(x[(i - 1) * m + j], x[i * m + j - 1]) == x[(i - 1) * m + j])
						{
							x[m * i + j] = a[m * i + j];
							x[i * m + j] += x[(i - 1) * m + j];
							cx[i * m + j] = false;
						}
						else
						{
							x[m * i + j] = a[m * i + j];
							x[i * m + j] += x[i * m + j - 1];
							cx[i * m + j] = true;
						}
					}
				}

				cout << x[m * n - 1] << "\n";

				//test
				permn = n - 1;
				permm = m - 1;
				dx[n * m - 1] = true;

				for (size_t i = 0; i < n + m - 1; i++)
				{
					if (cx[permn * m + permm])
					{
						dx[permn * m + permm - 1] = true;
						permm -= 1;
					}
					else
					{
						dx[(permn - 1) * m + permm] = true;
						permn -= 1;
					}
				}
			}
			else
			{
			
				int xq = q[k * 2];
				int yq = q[k * 2 + 1];
				x[yq * m + xq] = INT32_MAX;
				
				if (xq<m-1)
				{
					x[yq * m + xq + 1] = a[yq * m + xq + 1] + x[yq * m - m + xq + 1];
					for (size_t i = yq*m+xq+1; i < n*yq + m-1; i++)
					{
						if (min(x[i-m], x[i-1]) == x[i-m])
						{
							x[i] = a[i];
							x[i] += x[i - m];
							cx[i] = false;
						}
						else
						{
							x[i] = a[i];
							x[i] += x[i - 1];
							cx[i] = true;
						}
					}
					
				}
				if (yq<n-1)
				{
					x[yq * m + xq + m] = a[yq * m + xq + m] + x[yq * m + m + xq - 1];
					for (size_t i = yq * m + xq + m; i < n * m; i+=m)
					{
						if (min(x[i - m], x[i - 1]) == x[i - m])
						{
							
							x[i] = a[i];
							x[i] += x[i - m];
							cx[i] = false;
						}
						else
						{
							
							x[i] = a[i];
							x[i] += x[i - 1];
							cx[i] = true;
						}
					}
					
				}
				
				for (size_t i = yq+1; i < n; i++)
				{
					
					for (size_t j = xq+1; j < m; j++)
					{
						if (min(x[i*m+j - m], x[i*m+j - 1]) == x[i*m+j - m])
						{
							x[i*m+j] = a[i*m+j];
							x[i*m+j] += x[i*m+j - m];
							cx[i*m+j] = false;
						}
						else
						{
							x[i*m+j] = a[i*m+j];
							x[i*m+j] += x[i*m+j - 1];
							cx[i*m+j] = true;
						}
					}
				}
				





				cout << x[m * n - 1] << "\n";
				/*permn = n - 1;
				permm = m - 1;
				dx[n * m - 1] = true;

				for (size_t i = 0; i < n + m - 1; i++)
				{
					if (cx[permn * m + permm])
					{
						dx[permn * m + permm - 1] = true;
						permm -= 1;
					}
					else
					{
						dx[(permn - 1) * m + permm] = true;
						permn -= 1;
					}
				}
				for (size_t i = 0; i < n; i++)
				{
					for (size_t j = 0; j < m; j++)
					{
						if (dx[i * m + j]) cout << "1";
						else cout << "0";
					}
					cout << "\n";
				}
				for (size_t i = 0; i < n; i++)
				{
					for (size_t j = 0; j < m; j++)
					{
						cout << x[i * m + j] << " ";
					}
					cout << "\n";
				}*/
			
				
				
			}
		}
	}
}