#include <iostream>
#include <vector>

using namespace std;

long int n,m,q,zx,zy;
long long int a[1000][1000];
long long int globalniRez=999999999,najbRez;

void f(long int x,long int y,long long int rez)
{
	if((x==zx && y==zy)||(rez>=globalniRez)||(x>=n) || (y>=m)){return;}
	if(x==n-1 && y==m-1 && rez<globalniRez){
		globalniRez=rez;
	}
	if(a[x+1][y]<a[x][y+1])
	{
		f(x+1,y,rez+a[x+1][y]);
		f(x,y+1,rez+a[x][y+1]);
	}
	else{
		f(x,y+1,rez+a[x][y+1]);
		f(x+1,y,rez+a[x+1][y]);
	}
}

int main()
{
	cin >> n >> m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin >> a[i][j];
		}
	}
	cin >> q;
	zx=n;
	zy=m;
	f(0,0,a[0][0]);
	najbRez=globalniRez;
	globalniRez=9999999999;
	for(int i=0;i<q;i++)
	{
		cin >> zx >> zy;
		zx--;
		zy--;
		if(zx<n && zy<m){f(0,0,a[0][0]);cout << globalniRez << endl;}
		else{
			cout << najbRez << endl;
		}
		globalniRez=999999999;
	}
	return 0;
}