#include <algorithm>
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;
void PR(vector <vector <int> > vec)
{
	
	for (int i = 0; i < vec.size(); i++)
	{
		cout << "|";
		for (int j = 0; j < vec[i].size(); j++)
		{
			cout << " " << vec[i][j] << " |";
		}
		cout << endl;
	}
}

vector <int> kom;int n, m;
void GPS(vector <vector <int> > gr, int px, int py, int cd, int zx, int zy)
{
	//cout << "(" << px << ", " << py<< ", " << cd <<")" << endl;
	if (px == zx && py == zy)
		return;
	
	cd += gr[py][px];
	
	//m----x n-----y
	if (px == m - 1 && py == n - 1)
	{
		//cout << "Nasao:" << cd << endl;
		kom.push_back(cd);
		return;
	}

	if (px == m - 1)
		GPS(gr, px, py + 1, cd, zx, zy);
	else
	{
		GPS(gr, px + 1, py, cd, zx, zy);
		if(py != n-1)
			GPS(gr, px, py + 1, cd, zx, zy);
	}
}

void JankoCantCommunicate()
{
	
	cin >> n >> m;
	vector <vector <int> > doha;
	for (size_t i = 0; i < n; i++)
	{
		vector <int> tem;
		for (size_t j = 0; j < m; j++)
		{
			int y;
			cin >> y;
			tem.push_back(y);
		}
		doha.push_back(tem);
	}
	int q;
	cin >> q;
	vector <pair <int, int> > sl;
	for (size_t i = 0; i < q; i++)
	{
		pair <int,int> a;
		cin >> a.second >> a.first;
		sl.push_back(a);
	}
	for (size_t i = 0; i < q; i++)
	{
		kom.clear();
		GPS(doha, 0, 0, 0, sl[i].first - 1, sl[i].second - 1);
		sort(kom.begin(),kom.end());
		cout << kom[0] << endl;
	}
}


int main()
{
	JankoCantCommunicate();
	return 0;
}