#include <bits/stdc++.h>

using namespace std;
int N; 
int M; 

vector <vector<int>> tabla;
vector <vector<int>> navijaci;


void obidji(int r, int k, int potez)
{

	if (tabla[r][k] == -2) return;

	if (tabla[r][k] == -1)
		tabla[r][k] = potez;
	else if (potez >= tabla[r][k]) return;

	tabla[r][k] = potez;
	if (not(r + 1 > N - 1)) 
		obidji(r + 1, k, potez + navijaci[r + 1][k]);
	if (not(k + 1 > M - 1)) 
		obidji(r, k + 1, potez + navijaci[r][k + 1]);
}

int main()
{
	cin >> N >> M;
	tabla.resize(N, vector<int>(M));
	navijaci.resize(N, vector<int>(M));
	int i, j;

	for (i = 0; i < N; i++){
		for (j = 0; j < M; j++){
			cin >> navijaci[i][j];
		}
	}
	int Q;
	cin >> Q;

	for (int s = 0; s < Q; s++){
		for (i = 0; i < N; i++){
			for (j = 0; j < M; j++){
				tabla[i][j] = -1;
			}
		}
		int red, kolona;
		cin >> red;
		cin >> kolona;
		tabla[red-1][kolona-1] = -2;
		obidji(0, 0, navijaci[0][0]);
		std::cout << tabla[N - 1][M - 1] << endl;
	}

	return 0;
}