#include <iostream>
using namespace std;

#define MAX_INT 2147483647


int main()
{
	int N, M, Q;
	cin>>N>>M;
	long int** a = new long int*[N];
	long long** q = new long long*[N];
	
	for(int i=0; i<N; i++){
		a[i] = new long int[M];
		q[i] = new long long[M]{0};

		
		for(int j=0; j<M; j++)
			cin>>a[i][j];
			
	}
	
	cin>>Q;
	int* x = new int[Q];
	int* y = new int[Q];
	for(int i=0; i<Q; i++)
		cin>>x[i]>>y[i];
		

		
	for(int z=0; z<Q; z++) {
		int ib = x[z]-1, jb = y[z]-1;
		
		q[0][0] = a[0][0];
		
		
		
		for(int i=1; i<N; i++){
			if(i>=ib&&jb==0)
				q[i][0]=MAX_INT;
			else
				q[i][0] = a[i][0] + q[i-1][0];
		}
			
		
		for(int j=1; j<M; j++) {
			if(ib==0&&j>=jb)
				q[0][j]=MAX_INT;
			else
				q[0][j] = a[0][j] + q[0][j-1];
		}
		
		for(int i=1; i<N; i++) {
			for(int j=1; j<M; j++){
				if(i==ib && j==jb)
					q[i][j]=MAX_INT;
				else{
					if(q[i-1][j] < q[i][j-1])
						q[i][j] = a[i][j] + q[i-1][j];
					else
						q[i][j] = a[i][j] + q[i][j-1];					
				}

			}
		}

				
		cout<<q[N-1][M-1];
		if(z<Q-1)
			cout<<endl;
	}

	
	return 0;
}