#include <iostream>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <fstream>
#include <string>
#include <queue>

int min(int a, int b) {
	return (a < b) ? a : b;
}

int main()
{
	std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0);
	int n, k;
	std::cin >> n >> k;

	std::vector<std::vector<int>> a(n, std::vector<int>(k));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < k; j++) {
			std::cin >> a[i][j];
		}
	}
	int q;
	std::cin >> q;
	for (int v = 0; v < q; v++) {
		int x, y;
		std::cin >> y >> x;
		y--;
		x--;
		std::queue<std::pair<int, int>> queue;
		std::vector<std::vector<int>> s(n, std::vector<int>(k, 0));
		s[0][0] = a[0][0];
		queue.push({ 0, 0 });

		while (!queue.empty()) {
			int y1 = queue.front().first;
			int x1 = queue.front().second;
			queue.pop();
			if (y1 == y && x1 == x) {
				continue;
			}
			if (y1 < a.size() - 1) {
				if (s[y1 + 1][x1] != 0) {
					s[y1 + 1][x1] = min(s[y1 + 1][x1], s[y1][x1] + a[y1 + 1][x1]);
				}
				else {
					s[y1 + 1][x1] = s[y1][x1] + a[y1 + 1][x1];
					queue.push({ y1 + 1, x1 });
				}
			}
			if (x1 < a[0].size() - 1) {
				if (s[y1][x1+1] != 0) {
					s[y1][x1 + 1] = min(s[y1][x1 + 1], s[y1][x1] + a[y1][x1 + 1]);
				}
				else {
					s[y1][x1+1] = s[y1][x1] + a[y1][x1+1];
					queue.push({ y1, x1 +1 });
				}
			}
		}
		std::cout << s[n - 1][k - 1] << std::endl;

	}

	return 0;
}