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

int main() {
	const int cn = 1005;
	int n, m, q, zip, zjp;
	int a[cn][cn];
    long long s[cn][cn];
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	s[1][1] = a[1][1];
	for (int i = 2; i <= n; i++)
		s[i][1] = a[i][1] + s[i - 1][1];
	for (int i = 2; i <= m; i++)
		s[1][i] = a[1][i] + s[1][i - 1];
	cin >> q;
	int* qa = new int[q];
	for (int k = 0; k < q; k++) {
		cin >> zip >> zjp;
        int z = a[zip][zjp];
		a[zip][zjp] = 1000000000 + 1;
		for (int i = 2; i <= n; i++) {
			for (int j = 2; j <= m; j++) {
				if (s[i][j - 1] > s[i - 1][j])
					s[i][j] = a[i][j] + s[i - 1][j];
				else
					s[i][j] = a[i][j] + s[i][j - 1];
			}
		}
        a[zip][zjp] = z;
		qa[k] = s[n][m];
	}
	for (int i = 0; i < q; i++)
		cout << qa[i] << endl;
}