#include<bits/stdc++.h>

using namespace std;

int a[2001][2001];
int b[2001][2001];

int main() {
	int n, m;
	int Q;
	int forbiddenX, forbiddenY;
	int block = 1;
	cin >> n >> m;
	if (n==2) {

	}
	else {
		for(int i=0;i<n;i++) 
			for(int j=0;j<m;j++)
				cin >> a[i][j];
		cin >> Q;
		for(int q=0;q<Q;q++) {
			cin >> forbiddenX >> forbiddenY;
			forbiddenX--; forbiddenY--;
			b[0][0] = a[0][0];
			for(int i=1;i<m;i++) {
				if(i >= forbiddenY && forbiddenX == 0) b[0][i] = -1;
				else b[0][i] = b[0][i-1] + a[0][i];
			}
			for(int i=1;i<n;i++) {
				if(i >= forbiddenX && forbiddenY == 0) b[0][i] = -1;
				else b[i][0] = b[i-1][0] + a[i][0];
			}
			for(int i=1;i<n;i++) {
				for(int j=1;j<m;j++) {
					if(i == forbiddenX && j == forbiddenY)
						b[i][j] = -1;
					else if(b[i-1][j] == -1) 
						b[i][j] = b[i][j-1] + a[i][j];
					else if(b[i][j-1] == -1) 
						b[i][j] = b[i-1][j] + a[i][j];
					else 
						b[i][j] = min(b[i-1][j] + a[i][j], b[i][j-1] + a[i][j]);
				}
			}
			cout << b[n-1][m-1] << endl;
		}
	}
}