import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.num = N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 def count(self): return self.num h, w = mi() a = [li() for _ in range(h)] q = ii() SXSY = [[v - 1 for v in li()] for _ in range(q)] cost = [] for i in range(h): for j in range(w): cost.append((i, j, a[i][j])) cost.sort(key = lambda x: x[2]) ok = [h * w] * q ng = [0] * q dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] dz = 'UDLR' for _ in range(20): mid = [0] * q for i in range(q): mid[i] = (ok[i] + ng[i]) // 2 MID = [[] for _ in range(h * w)] for i in range(q): MID[mid[i]].append(i) U = dsu(h * w) for i in range(h * w): x, y, c = cost[i] for k in range(4): tox = dx[k] + x toy = dy[k] + y if 0 <= tox < h and 0 <= toy < w and a[tox][toy] <= c: U.merge(x * w + y, tox * w + toy) for j in MID[i]: sx, sy, tx, ty = SXSY[j] if U.same(sx * w + sy, tx * w + ty): ok[j] = mid[j] else: ng[j] = mid[j] for i in range(q): print(cost[ok[i]][2])