#include <iostream>

using namespace std;
long int n,m,q;
long long int a[10000][10000];
long long int globalniRez=999999999,zx,zy;

void f(long int x,long int y,long long int rez)
{
	if((x==zx && y==zy)||(rez>=globalniRez)){return;}
	
	if(x==n-1 && y==m-1 && rez<globalniRez){
		globalniRez=rez;
	}
	if(y+1<m)
	{
		f(x,y+1,rez+a[x][y+1]);
	}
	if(x+1<n)
	{
		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;
	long long int r[q];
	for(int i=0;i<q;i++)
	{
		cin >> zx >> zy;
		zx--;
		zy--;
		f(0,0,a[0][0]);
		r[i]=globalniRez;
		globalniRez=999999999;
	}
	for(int i=0;i<q;i++)
	{
		cout << r[i] << endl;
	}
	return 0;
}