We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).
The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.
You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.
You now have to take Q practical tests of your ability as a magical girl.
The i-th test will be conducted as follows:
 - Initially, a piece is placed on the square where the integer L_i is written.
 - Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.
 - Here, it is guaranteed that R_i-L_i is a multiple of D.
For each test, find the sum of magic points consumed during that test.

-----Constraints-----
 - 1 \leq H,W \leq 300
 - 1 \leq D \leq H×W
 - 1 \leq A_{i,j} \leq H×W
 - A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))
 - 1 \leq Q \leq 10^5
 - 1 \leq L_i \leq R_i \leq H×W
 - (R_i-L_i) is a multiple of D.

-----Input-----
Input is given from Standard Input in the following format:
H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

-----Output-----
For each test, print the sum of magic points consumed during that test.
Output should be in the order the tests are conducted.

-----Sample Input-----
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8

-----Sample Output-----
5

 - 4 is written in Square (1,2).
 - 6 is written in Square (3,3).
 - 8 is written in Square (3,1).
Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Difficulty: MEDIUM

Solutions:
I = lambda : map(int, input().split())
(h, w, d) = I()
H = {}
W = {}
for i in range(1, h + 1):
	for (j, k) in enumerate(I(), 1):
		(H[k], W[k]) = (i, j)
x = 1 + h * w
s = [0 for _ in [0] * x]
for i in range(d + 1, x):
	s[i] = s[i - d] + abs(H[i] - H[i - d]) + abs(W[i] - W[i - d])
for _ in [0] * int(input()):
	(a, b) = I()
	print(s[b] - s[a])


(h, w, D) = map(int, input().split())
a = [[int(i) for i in input().split()] for _ in range(h)]
px = [0] * (h * w + 1)
py = [0] * (h * w + 1)
d = [0] * (h * w + 1)
for i in range(h):
	for j in range(w):
		px[a[i][j]] = i
		py[a[i][j]] = j
for i in range(D + 1, h * w + 1):
	d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(d[r] - d[l])


(H, W, D) = map(int, input().split())
P = [None] * (H * W + 1)
for h in range(H):
	A = list(map(int, input().split()))
	for w in range(W):
		P[A[w]] = [h, w]
B = [0] * (H * W + 1)
for n in range(D + 1, H * W + 1):
	B[n] = B[n - D] + abs(P[n][0] - P[n - D][0]) + abs(P[n][1] - P[n - D][1])
Q = int(input())
ans = 0
for q in range(Q):
	(L, R) = map(int, input().split())
	print(B[R] - B[L])


import sys

def Is():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())
(H, W, D) = Is()
d = dict()
for i in range(H):
	s = Is()
	for (j, e) in enumerate(s):
		d[e] = (i, j)
m = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	m[i] = m[i - D] + abs(d[i - D][0] - d[i][0]) + abs(d[i - D][1] - d[i][1])
Q = I()
for q in range(Q):
	(l, r) = Is()
	print(m[r] - m[l])


(H, W, D) = map(int, input().split())
Point = {}
for y in range(H):
	line = list(map(int, input().split()))
	for (x, p) in enumerate(line):
		Point[p] = (y, x)
Q = int(input())

def dist(ny, nx, py, px):
	return abs(ny - py) + abs(nx - px)
Cost = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	(yi, xi) = Point[i - D]
	(yj, xj) = Point[i]
	Cost[i] = Cost[i - D] + dist(yi, xi, yj, xj)
for i in range(Q):
	(l, r) = map(int, input().split())
	print(Cost[r] - Cost[l])


import sys
import math
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().strip()
NI = lambda : int(input())
NMI = lambda : map(int, input().split())
NLI = lambda : list(NMI())
SI = lambda : input()

def make_grid(h, w, num):
	return [[int(num)] * w for _ in range(h)]

def main():
	(H, W, D) = NMI()
	grid = [NLI() for i in range(H)]
	Q = NI()
	querys = [NLI() for i in range(Q)]
	point = [[-1, -1] for _ in range(H * W + 1)]
	for h in range(H):
		for w in range(W):
			a = grid[h][w]
			point[a] = [h, w]
	cost_S = [0] * (H * W + 1)
	for c in range(H * W + 1):
		if c <= D:
			continue
		cost_S[c] = cost_S[c - D] + abs(point[c][0] - point[c - D][0]) + abs(point[c][1] - point[c - D][1])
	for query in querys:
		(l, r) = query
		print(cost_S[r] - cost_S[l])

def __starting_point():
	main()
__starting_point()


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
d = [0] * (H * W + 1)
g = [0] * (H * W + 1)
for i in range(H):
	for j in range(W):
		d[A[i][j]] = [i, j]
for i in range(1, H * W - D + 1):
	g[i + D] = abs(d[i + D][0] - d[i][0]) + abs(d[i + D][1] - d[i][1]) + g[i]
for q in range(Q):
	(l, r) = map(int, input().split())
	print(g[r] - g[l])


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
x = [0] * (h * w + 1)
y = [0] * (h * w + 1)
ans = [0] * (h * w + 1)
for i in range(h):
	for j in range(w):
		x[a[i][j]] = i
		y[a[i][j]] = j
for i in range(d + 1, h * w + 1):
	ans[i] = ans[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d])
q = int(input())
for i in range(q):
	(l, r) = map(int, input().split())
	print(ans[r] - ans[l])


import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy
(H, W, D) = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
l = []
for h in range(H):
	for w in range(W):
		l.append((A[h][w], w, h))
l.sort()
S = [0] * (H * W + D + 10)
for (i, x, y) in l:
	if i + D <= H * W:
		(_, xx, yy) = l[i + D - 1]
		S[i + D] = abs(xx - x) + abs(yy - y) + S[i]
Q = int(input())
for i in range(Q):
	(L, R) = list(map(int, input().split()))
	print(S[R] - S[L])


(H, W, D) = map(int, input().split())
A = [None] * (H * W + 1)
for i in range(H):
	a = [int(i) for i in input().split()]
	for (j, v) in enumerate(a):
		A[v] = (i + 1, j + 1)
Q = int(input())
LR = [[int(i) for i in input().split()] for _ in range(Q)]
c = [0] * (H * W + 1)
for i in range(1 + D, W * H + 1):
	c[i] = c[i - D] + abs(A[i][0] - A[i - D][0]) + abs(A[i][1] - A[i - D][1])
for (l, r) in LR:
	print(c[r] - c[l])


(H, W, D) = map(int, input().split())
A = {}
for i in range(H):
	for (j, s) in enumerate(input().split()):
		A[int(s)] = [i, j]
Q = int(input())
exam = []
for i in range(Q):
	exam.append(list(map(int, input().split())))
grid = [0]
manta = lambda x, y: abs(x[0] - y[0]) + abs(x[1] - y[1])
for i in range(1, H * W + 1):
	if (i - 1) // D == 0:
		grid.append(0)
	else:
		grid.append(manta(A[i], A[i - D]) + grid[i - D])
for (l, r) in exam:
	print(grid[r] - grid[l])


def main():
	from itertools import accumulate
	(h, w, d) = list(map(int, input().split()))
	a = [0] * (h * w + 1)
	b = [0] * (h * w + 1)
	for i in range(1, h + 1):
		l = list(map(int, input().split()))
		for (j, k) in enumerate(l, 1):
			a[k] = i
			b[k] = j
	c = [0] * (h * w + 1)
	for i in range(d + 1, h * w + 1):
		c[i] = c[i - d] + abs(a[i] - a[i - d]) + abs(b[i] - b[i - d])
	q = int(input())
	res = []
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		res.append(c[r] - c[l])
	ans = '\n'.join(map(str, res))
	print(ans)

def __starting_point():
	main()
__starting_point()


(h, w, d) = map(int, input().split())
from collections import defaultdict
G = defaultdict(list)
for i in range(h):
	l = list(map(int, input().split()))
	for (n, item) in enumerate(l):
		G[item] = [n, i]
dp = [0 for i in range(h * w + 1)]
for i in range(d):
	current_place = i + 1
	while current_place + d <= h * w:
		(x, y) = G[current_place]
		next_place = current_place + d
		(x1, y1) = G[next_place]
		magic = abs(x - x1) + abs(y - y1)
		dp[next_place] = magic + dp[current_place]
		current_place = next_place
q = int(input())
for i in range(q):
	(l, r) = map(int, input().split())
	print(dp[r] - dp[l], flush=True)


(h, w, d) = map(int, input().split())
box = [list(map(int, input().split())) for i in range(h)]
adress = {}
for i in range(h):
	for j in range(w):
		adress[box[i][j]] = [i, j]
q = int(input())
order = [list(map(int, input().split())) for i in range(q)]
S = [0] * (h * w + 1)
for k in range(d + 1, h * w + 1):
	(x, y) = adress[k]
	(i, j) = adress[k - d]
	S[k] = S[k - d] + abs(x - i) + abs(y - j)
for (x, y) in order:
	print(S[y] - S[x])


(h, w, d) = map(int, input().split(' '))
comp = {}
for i in range(h):
	tmp = list(map(int, input().split(' ')))
	for j in range(w):
		comp[tmp[j]] = [i, j]
q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]
dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	cnt = dp[i - d]
	cnt += abs(comp[i][0] - comp[i - d][0])
	cnt += abs(comp[i][1] - comp[i - d][1])
	dp[i] = cnt
for (l, r) in lr:
	print(dp[r] - dp[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
P = [(0, 0)] * (H * W + 1)
for i in range(H):
	for j in range(W):
		P[A[i][j]] = (i, j)
d = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	d[i] = d[i - D] + abs(P[i][0] - P[i - D][0]) + abs(P[i][1] - P[i - D][1])
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(d[R] - d[L])


(H, W, D) = [int(i) for i in input().split()]
px = [0] * 90001
py = [0] * 90001
for i in range(H):
	a = [int(i) for i in input().split()]
	for (j, v) in enumerate(a):
		px[v] = j
		py[v] = i
d = [0] * 90001
for i in range(D + 1, 90001):
	d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
Q = int(input())
for i in range(Q):
	(L, R) = map(int, input().split())
	ans = d[R] - d[L]
	print(ans)


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
dic = {}
q = int(input())
for i in range(h):
	for j in range(w):
		dic[a[i][j]] = (i, j)
li = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	if i - d not in dic:
		continue
	li[i] = li[i - d] + abs(dic[i][0] - dic[i - d][0]) + abs(dic[i][1] - dic[i - d][1])
for _ in range(q):
	(l, r) = map(int, input().split())
	print(li[r] - li[l])


(h, w, d) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]
q = int(input())
LR = [list(map(int, input().split())) for _ in range(q)]
B = [[] for _ in range(h * w + 1)]
for i in range(h):
	for j in range(w):
		B[A[i][j]] = [i, j]
DP = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	(x, y) = B[i]
	(z, w) = B[i - d]
	DP[i] = DP[i - d] + abs(x - z) + abs(y - w)
for (l, r) in LR:
	print(DP[r] - DP[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
x = [0] * (H * W + 1)
p = [[] for _ in range(H * W + 1)]
for h in range(H):
	for w in range(W):
		p[A[h][w]] = [h, w]
for i in range(D + 1, H * W + 1):
	x[i] = x[i - D] + abs(p[i][0] - p[i - D][0]) + abs(p[i][1] - p[i - D][1])
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(x[r] - x[l])


(H, W, D) = map(int, input().split())
d = [[0, 0] for i in range(H * W + 1)]
d2 = [0 for i in range(H * W + 1)]
for j in range(H):
	a = [int(x) for x in input().split()]
	for k in range(W):
		d[a[k]] = [j + 1, k + 1]
for l in range(D + 1, H * W + 1):
	d2[l] = abs(d[l][0] - d[l - D][0]) + abs(d[l][1] - d[l - D][1])
for m in range(D + 1, H * W + 1):
	d2[m] += d2[m - D]
Q = int(input())
for i in range(Q):
	(a, b) = map(int, input().split())
	if a == b:
		print(0)
	else:
		ans = d2[b] - d2[a]
		print(ans)


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
r = [None for i in range(h * w + 1)]
for i in range(h):
	for j in range(w):
		r[a[i][j]] = (i, j)
c = [0 for i in range(h * w + 1)]
for i in range(d + 1, h * w + 1):
	c[i] = c[i - d] + abs(r[i][0] - r[i - d][0]) + abs(r[i][1] - r[i - d][1])
q = int(input())
for _ in range(q):
	(x, y) = map(int, input().split())
	print(c[y] - c[x])


(H, W, D) = map(int, input().split())
X = [0] * (H * W + 1)
for i in range(H):
	j = 0
	for a in input().split():
		X[int(a)] = (i, j)
		j += 1
S = [0] * (H * W + 1)
for i in range(1, H * W + 1):
	if i + D <= H * W:
		S[i + D] += S[i] + abs(X[i + D][0] - X[i][0]) + abs(X[i + D][1] - X[i][1])
Q = int(input())
for q in range(Q):
	(l, r) = map(int, input().split())
	print(S[r] - S[l])


(H, W, D) = map(int, input().split())
px = [0] * (H * W + 1)
py = [0] * (H * W + 1)
dp = [0] * (H * W + 1)
for i in range(H):
	inf = input().split()
	for j in range(W):
		a = int(inf[j])
		py[a] = i
		px[a] = j
for i in range(D + 1, H * W + 1):
	dp[i] = dp[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
Q = int(input())
for i in range(Q):
	(L, R) = map(int, input().split())
	print(dp[R] - dp[L])


(H, W, D) = list(map(int, input().split()))
(d, px, py) = ([0] * (H * W + 1), [0] * (H * W + 1), [0] * (H * W + 1))
for i in range(H):
	a = list(map(int, input().split()))
	for j in range(W):
		px[a[j]] = i
		py[a[j]] = j
for i in range(D + 1, H * W + 1):
	d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
Q = int(input())
for _ in range(Q):
	(l, r) = list(map(int, input().split()))
	print(d[r] - d[l])


(H, W, D) = map(int, input().split())
A_where = [0] * (H * W + 1)
A_where[0] = (-1, -1)
for p in range(H):
	A = list(map(int, input().split()))
	for q in range(W):
		A_where[A[q]] = (p, q)
Q = int(input())
DP = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	(y1, x1) = A_where[i]
	(y2, x2) = A_where[i - D]
	DP[i] = DP[i - D] + abs(y1 - y2) + abs(x1 - x2)
for i in range(Q):
	(l, r) = map(int, input().split())
	print(DP[r] - DP[l])


(h, w, d) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]
n = h * w
L = [0] * (n + 1)

def dis(p, q):
	return abs(p[0] - q[0]) + abs(p[1] - q[1])
for i in range(h):
	for j in range(w):
		t = A[i][j]
		L[t] = (i, j)
K = [0] * (n + 1)
for i in range(1, n - d + 1):
	K[i + d] = dis(L[i + d], L[i])
	K[i + d] += K[i]
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(K[r] - K[l])


(H, W, D) = list(map(int, input().split()))
map_ = [[0, 0] for _ in range(H * W + 1)]
for y in range(H):
	for (x, A) in enumerate(map(int, input().split())):
		map_[A] = [x + 1, y + 1]
cost = [0 for _ in range(H * W + 1)]
for i in range(D + 1, H * W + 1):
	cost[i] += abs(map_[i][0] - map_[i - D][0]) + abs(map_[i][1] - map_[i - D][1]) + cost[i - D]
Q = int(input())
for i in range(Q):
	(L, R) = list(map(int, input().split()))
	print(cost[R] - cost[L])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
P = [None] * (H * W + 1)
for i in range(H):
	for j in range(W):
		P[A[i][j]] = (i, j)
C = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	(pi, pj) = P[i]
	(qi, qj) = P[i - D]
	C[i] = C[i - D] + abs(pi - qi) + abs(pj - qj)
ans = []
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	ans.append(C[R] - C[L])
print(*ans, sep='\n')


(h, w, d) = map(int, input().split())
hw = [[] for _ in range(h * w + 1)]
for i in range(h):
	row = list(map(int, input().split()))
	for j in range(w):
		hw[row[j]] = [i, j]
a = [0 for _ in range(h * w + 1)]
for i in range(d + 1, h * w + 1):
	a[i] = a[i - d] + abs(hw[i][0] - hw[i - d][0]) + abs(hw[i][1] - hw[i - d][1])
q = int(input())
for i in range(q):
	(L, R) = map(int, input().split())
	print(a[R] - a[L])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
lr = [list(map(int, input().split())) for _ in range(Q)]
C = {}
for i in range(H):
	for j in range(W):
		C[A[i][j]] = [j, i]
S = [0] * (H * W + 1)
i = D + 1
while i < H * W + 1:
	(x1, y1) = (C[i][0], C[i][1])
	(x2, y2) = (C[i - D][0], C[i - D][1])
	S[i] = S[i - D] + abs(x2 - x1) + abs(y2 - y1)
	i += 1
for (l, r) in lr:
	q = S[r] - S[l]
	print(q)


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
q = int(input())
l = [list(map(int, input().split())) for _ in range(q)]
dic = {}
for i in range(len(a)):
	for j in range(len(a[i])):
		dic[a[i][j]] = (i, j)
sum = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	sum[i] = sum[i - d] + abs(dic[i][0] - dic[i - d][0]) + abs(dic[i][1] - dic[i - d][1])
for l_ in l:
	print(sum[l_[1]] - sum[l_[0]])


import sys
input = sys.stdin.readline
(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
x = [0] * (h * w + 1)
y = [0] * (h * w + 1)
for i in range(1, h + 1):
	for j in range(1, w + 1):
		x[a[i - 1][j - 1]] = i
		y[a[i - 1][j - 1]] = j
dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	dp[i] = dp[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d])
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(dp[r] - dp[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for i in range(Q)]
pos = [(-1, -1) for i in range(H * W + 1)]
for h in range(H):
	for w in range(W):
		pos[A[h][w]] = (h, w)
cost = [0] * (H * W + 1)
for i in range(1 + D, H * W + 1):
	cost[i] = cost[i - D] + abs(pos[i][0] - pos[i - D][0]) + abs(pos[i][1] - pos[i - D][1])
for q in range(Q):
	print(cost[LR[q][1]] - cost[LR[q][0]])


(h, w, d) = map(int, input().split())
dic = {}
for i in range(h):
	a = input().split()
	for j in range(w):
		dic[int(a[j])] = (i + 1, j + 1)
q = int(input())
c = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	(pi, pj) = dic[i]
	(qi, qj) = dic[i - d]
	c[i] = c[i - d] + abs(pi - qi) + abs(pj - qj)
for i in range(q):
	(l, r) = map(int, input().split())
	print(c[r] - c[l])


(H, W, D) = [int(x) for x in input().split()]
size = H * W + 1
c = [(-1, -1)] * size
p = [0] * size
for y in range(H):
	for (x, a) in enumerate([int(s) for s in input().split()]):
		c[a] = (x, y)
for a in range(D + 1, size):
	p[a] = p[a - D] + abs(c[a][0] - c[a - D][0]) + abs(c[a][1] - c[a - D][1])
Q = int(input())
for i in range(Q):
	(L, R) = [int(lr) for lr in input().split()]
	print(p[R] - p[L])


(h, w, d) = map(int, input().split())
a = []
box = [[] for i in range(h * w + 1)]
wi = 0
for i in range(h):
	ai = list(map(int, input().split()))
	for j in range(w):
		box[ai[j]] = [i, j]
p = [0]
for i in range(1, h * w + 1):
	if i - d > 0:
		p.append(p[i - d] + abs(box[i][0] - box[i - d][0]) + abs(box[i][1] - box[i - d][1]))
	else:
		p.append(0)
q = int(input())
for i in range(q):
	(l, r) = map(int, input().split())
	print(p[r] - p[l])


def getlist():
	return list(map(int, input().split()))
(H, W, D) = getlist()
a = []
mp = [0] * (H * W + 1)
for i in range(H):
	A = getlist()
	for j in range(W):
		a.append([A[j], i, j])
a = sorted(a)
for i in range(D, H * W):
	mp[i] += mp[i - D] + abs(a[i][1] - a[i - D][1]) + abs(a[i][2] - a[i - D][2])
Q = int(input())
for i in range(Q):
	(L, R) = getlist()
	print(mp[R - 1] - mp[L - 1])


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
e = [[] for _ in range(h * w + 1)]
dp = [0] * (h * w + 1)
for i in range(h):
	for j in range(w):
		e[a[i][j]] = (i, j)
for i in range(1, h * w + 1 - d):
	dp[i + d] = dp[i] + abs(e[i + d][0] - e[i][0]) + abs(e[i + d][1] - e[i][1])
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(dp[r] - dp[l])


(h, w, d) = map(int, input().split())
a = {}
for y in range(h):
	tmp = list(map(int, input().split()))
	for (x, v) in enumerate(tmp):
		a[v] = (y, x)
dis = [0] * (h * w + 1)
for i in range(1, h * w + 1):
	if i > d:
		bk = a[i - d]
		nx = a[i]
		dis[i] = dis[i - d] + abs(bk[0] - nx[0]) + abs(bk[1] - nx[1])
q = int(input())
ans = []
for _ in range(q):
	(start, goal) = map(int, input().split())
	ans.append(dis[goal] - dis[start])
for item in ans:
	print(item)


(h, w, d) = map(int, input().split())
dp = [0] * (h * w + 1)
ab = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
	for j in range(w):
		s = ab[i][j]
		dp[s] = [i, j]
dpp = [0] * (h * w + 1)
for k in range(d + 1, h * w + 1):
	dpp[k] = dpp[k - d] + abs(dp[k][0] - dp[k - d][0]) + abs(dp[k][1] - dp[k - d][1])
q = int(input())
for x in range(q):
	(a, b) = map(int, input().split())
	print(dpp[b] - dpp[a])


def get_dist(cur, nxt):
	return abs(cur[0] - nxt[0]) + abs(cur[1] - nxt[1])
(H, W, D) = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
pos = {}
for i in range(H):
	for j in range(W):
		pos[A[i][j]] = (i, j)
acc = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	acc[i] = acc[i - D] + get_dist(pos[i - D], pos[i])
Q = int(input())
for _ in range(Q):
	ans = 0
	(L, R) = list(map(int, input().split()))
	print(acc[R] - acc[L])


import sys
from itertools import accumulate
input = sys.stdin.readline

def dfs(H, W, D, A, Q, LR):
	position = [(-1, -1)] * (H * W + 1)
	for h in range(H):
		for w in range(W):
			position[A[h][w]] = (h, w)
	mp = [0] * (H * W + 1)
	for i in range(1, H * W + 1 - D):
		(h, w) = position[i]
		(y, x) = position[i + D]
		mp[i + D] = abs(x - w) + abs(y - h)
	for d in range(1, D + 1):
		mp[d::D] = list(accumulate(mp[d::D]))
	ans = [mp[r] - mp[l] for (l, r) in LR]
	return ans

def main():
	(H, W, D) = list(map(int, input().split()))
	A = [None] * H
	for i in range(H):
		A[i] = tuple(map(int, input().split()))
	Q = int(input())
	LR = [None] * Q
	for i in range(Q):
		LR[i] = tuple(map(int, input().split()))
	ans = [0] * Q
	ans = dfs(H, W, D, A, Q, LR)
	print('\n'.join(map(str, ans)))

def __starting_point():
	main()
__starting_point()


(H, W, D) = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
Q = int(input())
L = [0] * (H * W + 1)
for i in range(H):
	for j in range(W):
		L[A[i][j]] = [i, j]
E = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	E[i] = E[i - D] + abs(L[i][0] - L[i - D][0]) + abs(L[i][1] - L[i - D][1])
for i in range(Q):
	ans = 0
	(l, r) = map(int, input().split())
	ans = E[r] - E[l]
	print(ans)


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
px = [0] * (H * W + 1)
py = [0] * (H * W + 1)
for y in range(H):
	for x in range(W):
		n = A[y][x]
		px[n] = x + 1
		py[n] = y + 1
csum = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	csum[i] = csum[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
for q in range(Q):
	(L, R) = map(int, input().split())
	print(csum[R] - csum[L])


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
pos = {a[i][j]: (i, j) for i in range(h) for j in range(w)}
acc = [0] * (h * w + 1)
for v in range(d + 1, h * w + 1):
	(i, j) = pos[v]
	(x, y) = pos[v - d]
	acc[v] = acc[v - d] + abs(x - i) + abs(y - j)
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(acc[r] - acc[l])


(h, w, d) = map(int, input().split())
l = [[0]] * (h * w + 1)
for i in range(h):
	temp = list(map(int, input().split()))
	for j in range(len(temp)):
		l[temp[j]] = [i + 1, j + 1]
c = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	(pi, pj) = l[i]
	(qi, qj) = l[i - d]
	c[i] = c[i - d] + abs(pi - qi) + abs(pj - qj)
q = int(input())
for i in range(q):
	(l, r) = map(int, input().split())
	print(c[r] - c[l])


(h, w, d) = map(int, input().split())
b = [0] * (h * w)
for i in range(h):
	for (j, s) in enumerate(input().split()):
		b[int(s) - 1] = (i, j)
a = [0] * (h * w + d)
for i in range(d, h * w):
	a[i] = a[i - d] + abs(b[i][0] - b[i - d][0]) + abs(b[i][1] - b[i - d][1])
for i in range(int(input())):
	(l, r) = map(int, input().split())
	print(a[r - 1] - a[l - 1])


(h, w, d) = map(int, input().split())
ad = {}
for i in range(h):
	line = list(map(int, input().split()))
	for j in range(w):
		ad[line[j]] = (i, j)
q = int(input())
lr = [list(map(int, input().split())) for _ in range(q)]
cl = [0] * (h * w + 1)
for i in range(1, h * w + 1):
	if i + d < len(cl):
		f = ad[i]
		t = ad[i + d]
		cl[i + d] = cl[i] + abs(f[0] - t[0]) + abs(f[1] - t[1])
for (l, r) in lr:
	print(cl[r] - cl[l])


(H, W, D) = map(int, input().split())
A = [0] * H
for i in range(H):
	A[i] = list(map(int, input().split()))
dict = {}
for i in range(H):
	for j in range(W):
		dict[A[i][j]] = [i, j]
dp = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
	dp[k] = dp[k - D] + abs(dict[k][0] - dict[k - D][0]) + abs(dict[k][1] - dict[k - D][1])
Q = int(input())
for i in range(Q):
	(L, R) = map(int, input().split())
	print(dp[R] - dp[L])


(H, W, D) = map(int, input().split())
px = [0] * (H * W + 1)
py = [0] * (H * W + 1)
d = [0] * (H * W + 1)
for y in range(H):
	line = list(map(int, input().split()))
	for (x, A) in enumerate(line):
		px[A] = x
		py[A] = y
for i in range(D + 1, H * W + 1):
	d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(d[R] - d[L])


import sys
input = sys.stdin.readline
(h, w, d) = map(int, input().split())
p = dict()
for i in range(h):
	for (j, x) in enumerate(map(int, input().split())):
		p[x] = (i, j)
dp = [0] * (h * w + 1)
for i in range(h * w + 1):
	if d < i:
		dp[i] = dp[i - d] + abs(p[i][0] - p[i - d][0]) + abs(p[i][1] - p[i - d][1])
q = int(input())
for i in range(q):
	(a, b) = map(int, input().split())
	ans = dp[b] - dp[a]
	print(ans)


(H, W, D) = list(map(int, input().split()))
A = [list(map(int, input().split())) for i in range(H)]
P = {}
for h in range(H):
	for w in range(W):
		P[A[h][w]] = (h, w)
C = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	C[i] = C[i - D] + abs(P[i][0] - P[i - D][0]) + abs(P[i][1] - P[i - D][1])
Q = int(input())
for q in range(Q):
	(L, R) = list(map(int, input().split()))
	print(C[R] - C[L])


(h, w, d) = map(int, input().split())
g = [[] for _ in range(w)]
for _ in range(h):
	tmp_list = list(map(int, input().split()))
	for i in range(w):
		g[i].append(tmp_list[i])
q = int(input())
qs = [list(map(int, input().split())) for _ in range(q)]
loc = [0] * (h * w + 1)
for i in range(h):
	for j in range(w):
		loc[g[j][i]] = (i, j)
roots = [0] * (h * w + 1)
for i in range(1, d + 1):
	j = i + d
	while j < h * w + 1:
		cost = abs(loc[j][0] - loc[j - d][0]) + abs(loc[j][1] - loc[j - d][1])
		roots[j] = cost + roots[j - d]
		j += d
for i in qs:
	(l, r) = (i[0], i[1])
	print(roots[r] - roots[l])


import sys
input = sys.stdin.readline
(h, w, d) = map(int, input().split())
num = h * w
x = [0] * (num + 1)
y = [0] * (num + 1)
mp = [0] * (num + 1)
for i in range(h):
	for (j, a) in enumerate(map(int, input().split())):
		(x[a], y[a]) = [i + 1, j + 1]
for i in range(d + 1, num + 1):
	mp[i] = mp[i - d] + abs(x[i] - x[i - d]) + abs(y[i] - y[i - d])
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(mp[r] - mp[l])


def main():
	import sys
	input = sys.stdin.readline
	(H, W, D) = [int(x) for x in input().strip().split()]
	M = [0] * H
	C = {}
	revM = {}
	for h in range(H):
		M[h] = [int(x) for x in input().strip().split()]
		for (i, w) in enumerate(M[h]):
			revM[w] = (h, i)
	Q = int(input())
	HW = H * W
	ans = [0] * (HW + 1)
	for i in range(1, D + 1):
		cur = i
		while cur + D <= HW:
			ans[cur + D] += ans[cur] + abs(revM[cur][0] - revM[cur + D][0]) + abs(revM[cur][1] - revM[cur + D][1])
			cur += D
	for q in range(Q):
		(l, r) = [int(x) for x in input().strip().split()]
		print(ans[r] - ans[l])

def __starting_point():
	main()
__starting_point()


(H, W, D) = map(int, input().split())
pos = {}
for i in range(H):
	A = list(map(int, input().split()))
	for j in range(W):
		pos[A[j]] = (i, j)
p = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	p[i] = p[i - D] + abs(pos[i][0] - pos[i - D][0]) + abs(pos[i][1] - pos[i - D][1])
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(p[R] - p[L])


(H, W, D) = map(int, input().split())
A = {}
for i in range(H):
	row = list(map(int, input().split()))
	for j in range(W):
		A[row[j]] = [i + 1, j + 1]
Q = int(input())
ANS = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	ANS[i] = ANS[i - D] + abs(A[i][0] - A[i - D][0]) + abs(A[i][1] - A[i - D][1])
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(ANS[R] - ANS[L])


(h, w, d) = map(int, input().split())
m = {}
for i in range(1, h + 1):
	for (j, k) in enumerate(map(int, input().split())):
		m[k] = (i, j + 1)
s = [0 for _ in range(1 + h * w)]
for i in range(d + 1, 1 + h * w):
	s[i] = s[i - d] + abs(m[i][0] - m[i - d][0]) + abs(m[i][1] - m[i - d][1])
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	print(s[b] - s[a])


import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
	(H, W, D) = list(map(int, input().split()))
	A = [list(map(int, input().split())) for _ in range(H)]
	x = [0] * (H * W + 1)
	p = [[] for _ in range(H * W + 1)]
	for h in range(H):
		for w in range(W):
			p[A[h][w]] = [h, w]
	for i in range(D + 1, H * W + 1):
		x[i] = x[i - D] + abs(p[i][0] - p[i - D][0]) + abs(p[i][1] - p[i - D][1])
	for _ in range(int(input())):
		(l, r) = list(map(int, input().split()))
		print(x[r] - x[l])

def __starting_point():
	main()
__starting_point()


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
Q = int(input())
S = [list(map(int, input().split())) for i in range(Q)]
P = [None] * (H * W + 1)
for i in range(H):
	for j in range(W):
		P[A[i][j]] = [i, j]
Accum = [0] * (H * W + 1)
accum = 0
for i in range(D + 1, H * W + 1):
	Accum[i] = Accum[i - D] + abs(P[i][0] - P[i - D][0]) + abs(P[i][1] - P[i - D][1])
for s in S:
	print(Accum[s[1]] - Accum[s[0]])


dic = {}
(h, w, d) = map(int, input().split())
lis = [list(input().split()) for _ in range(h)]
for i in range(h):
	for j in range(w):
		dic[int(lis[i][j])] = (i, j)
n = h * w
su = [0] * (n + 1)
for k in range(1, n - d + 1):
	x = dic[k + d][0] - dic[k][0]
	y = dic[k + d][1] - dic[k][1]
	su[k + d] = su[k] + abs(x) + abs(y)
q = int(input())
for i in range(q):
	(s, g) = map(int, input().split())
	print(su[g] - su[s])


(h, w, d) = map(int, input().split())
ans = [0 for _ in range(h * w + 1)]
za = [[] for _ in range(h * w + 1)]
for i in range(h):
	a = list(map(int, input().split()))
	for x in range(w):
		za[a[x]].append(i)
		za[a[x]].append(x)
for x in range(1, h * w + 1):
	if x + d <= w * h:
		ans[x + d] = ans[x] + abs(za[x + d][0] - za[x][0]) + abs(za[x + d][1] - za[x][1])
q = int(input())
for _ in range(q):
	(e, f) = map(int, input().split())
	print(ans[f] - ans[e])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in [0] * H]
Q = int(input())
LR = [list(map(int, input().split())) for _ in [0] * Q]
N = H * W
C = [0] * N
pos = [(0, 0)] * N
for i in range(H):
	for j in range(W):
		a = A[i][j] - 1
		pos[a] = (i, j)
for i in range(D, N):
	C[i] = sum((abs(pos[i][xy] - pos[i - D][xy]) for xy in range(2)))
S = [0] * (N + 1)
for i in range(D, N + 1):
	S[i] = C[i - 1] + S[i - D]
for (l, r) in LR:
	print(S[r] - S[l])


(h, w, d) = map(int, input().split())
coordinates = {}
for i in range(h):
	row = list(map(int, input().split()))
	for j in range(w):
		coordinates[row[j]] = (i, j)
costs = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	(x1, y1) = coordinates[i]
	(x2, y2) = coordinates[i - d]
	costs[i] = abs(x1 - x2) + abs(y1 - y2) + costs[i - d]
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(costs[r] - costs[l])


(H, W, D) = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]
P = {}
for i in range(H):
	for j in range(W):
		P[A[i][j] - 1] = (i, j)
cum = [0] * (H * W + 1)
for a in range(H * W - D):
	(i, j) = P[a]
	(x, y) = P[a + D]
	cum[a + D] = cum[a] + abs(x - i) + abs(y - j)
Q = int(input())
ans = []
for _ in range(Q):
	(l, r) = map(int, input().split())
	ans.append(cum[r - 1] - cum[l - 1])
print('\n'.join(map(str, ans)))


(H, W, D) = map(int, input().split())
dic = dict()
for i in range(H):
	A = list(map(int, input().split()))
	for j in range(W):
		dic[A[j]] = (i, j)
Q = int(input())
d = [0 for i in range(H * W + 1)]
for i in range(D + 1, H * W + 1):
	(p, q) = dic[i - D]
	(x, y) = dic[i]
	d[i] = d[i - D] + abs(p - x) + abs(q - y)
for _ in range(Q):
	ans = 0
	(L, R) = map(int, input().split())
	print(d[R] - d[L])


(H, W, D) = map(int, input().split())
m = [None] * (H * W + 1)
s = [0] * (H * W + 1)
for y in range(H):
	A = list(map(int, input().split()))
	for (x, a) in enumerate(A):
		m[a] = (x, y)
for i in range(D + 1, H * W + 1):
	(x, y) = m[i]
	(px, py) = m[i - D]
	s[i] = s[i - D] + abs(x - px) + abs(y - py)
Q = int(input())
for i in range(Q):
	(L, R) = map(int, input().split())
	print(s[R] - s[L])


(h, w, d) = map(int, input().split())
l = [list(map(int, input().split())) for i in range(h)]
L = [0 for i in range(h * w)]
for i in range(h):
	for j in range(w):
		L[l[i][j] - 1] = [i, j]
sumL = [0 for i in range(h * w + 1)]
for i in range(h * w):
	if i >= d:
		sumL[i + 1] = sumL[i + 1 - d] + abs(L[i][0] - L[i - d][0]) + abs(L[i][1] - L[i - d][1])
q = int(input())
QL = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
	print(sumL[QL[i][1]] - sumL[QL[i][0]])


(H, W, D) = map(int, input().split())
A = []
N = H * W
indices = [None] * (N + 1)
for h in range(H):
	tmp = list(map(int, input().split()))
	A.append(tmp)
	for w in range(W):
		indices[tmp[w]] = (h, w)
d = [0] * (N + 1)
for i in range(D + 1, N + 1):
	(y1, x1) = indices[i]
	(y2, x2) = indices[i - D]
	d[i] = d[i - D] + abs(y1 - y2) + abs(x1 - x2)
Q = int(input())
for _ in range(Q):
	(l, r) = map(int, input().split())
	print(d[r] - d[l])


import sys
(H, W, D) = map(int, sys.stdin.readline().split())
num = {}
for j in range(H):
	tmp = map(int, sys.stdin.readline().split())
	for (i, n) in enumerate(tmp):
		num[n] = (i, j)
S = [0 for _ in range(H * W + D)]
for i in range(D + 1, H * W + 1):
	(x, y) = num[i]
	(a, b) = num[i - D]
	S[i] = S[i - D] + abs(x - a) + abs(y - b)
Q = int(sys.stdin.readline())
for _ in range(Q):
	(L, R) = map(int, sys.stdin.readline().split())
	r = L % D
	print(S[R] - S[L])


(H, W, D) = map(int, input().split())
pos = [0] * (H * W)
Z = [0] * (H * W + 1)
for j in range(H):
	A = list(map(int, input().split()))
	for i in range(W):
		pos[A[i] - 1] = (i, j)
for k in range(D):
	for x in range(k, H * W - D, D):
		pos1 = pos[x]
		pos2 = pos[x + D]
		Z[x + D + 1] = abs(pos2[0] - pos1[0]) + abs(pos2[1] - pos1[1]) + Z[x + 1]
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	print(Z[y] - Z[x])


(h, w, d) = map(int, input().split())
p = [0] * (h * w + d * 2)
s = [0] * (h * w + d * 2)
for x in range(h):
	y = 1
	for a in map(int, input().split()):
		p[a] = [x, y]
		y += 1
for i in range(1, h * w + 1 - d):
	s[i + d] = s[i] + abs(p[i + d][0] - p[i][0]) + abs(p[i + d][1] - p[i][1])
for i in range(int(input())):
	(l, r) = map(int, input().split())
	print(s[r] - s[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for i in range(Q)]
d = [0] * (H * W + 1)
num_dict = {}
y = 0
for aa in A:
	x = 0
	for a in aa:
		num_dict[a] = [x, y]
		x += 1
	y += 1
for i in range(D + 1, H * W + 1):
	d[i] = d[i - D] + abs(num_dict[i][0] - num_dict[i - D][0]) + abs(num_dict[i][1] - num_dict[i - D][1])
for (l, r) in LR:
	print(d[r] - d[l])


(h, w, d) = map(int, input().split())
c = {}
for i in range(h):
	x = list(map(int, input().split()))
	for j in range(w):
		c[x[j]] = [i, j]
a = [0] * (h * w - d + 1)
b = [0] * (h * w + 1)
for k in range(1, h * w - d + 1):
	a[k] = abs(c[k + d][0] - c[k][0]) + abs(c[k + d][1] - c[k][1])
for k in range(1, h * w - d + 1):
	if k <= d:
		b[k + d] = a[k]
	else:
		b[k + d] = b[k] + a[k]
for _ in [0] * int(input()):
	(l, r) = map(int, input().split())
	print(b[r] - b[l])


(H, W, D) = map(int, input().split())
data = [0] * (H * W + D + 1)
for i in range(H):
	A = list(map(int, input().split()))
	for j in range(W):
		data[A[j] + D] = [i, j]
Q = int(input())
S = [0] * (H * W + D + 1)
for i in range(H * W):
	if i >= D:
		S[i + D + 1] = S[i + 1] + abs(data[i + D + 1][0] - data[i + 1][0]) + abs(data[i + D + 1][1] - data[i + 1][1])
for i in range(Q):
	(L, R) = map(int, input().split())
	print(S[R + D] - S[L + D])


(h, w, d) = map(int, input().split())
lst = [0 for i in range(h * w + 1)]
for i in range(h):
	x = list(map(int, input().split()))
	for j in range(w):
		lst[x[j]] = (i, j)
dif = [0 for i in range(h * w + 1)]
i = d + 1
while i <= w * h:
	dif[i] = dif[i - d] + abs(lst[i][0] - lst[i - d][0]) + abs(lst[i][1] - lst[i - d][1])
	i += 1
q = int(input())
ans = []
for i in range(q):
	(l, r) = map(int, input().split())
	ans.append(dif[r] - dif[l])
for num in ans:
	print(num)


(h, w, d) = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(h)]
pos = [0] * (h * w + 1)
for i in range(h):
	for j in range(w):
		pos[a[i][j]] = (j, i)
dis = [0] * (h * w + 1)
for i in range(h * w + 1):
	if i > d:
		(x1, y1) = pos[i - d]
		(x2, y2) = pos[i]
		dis[i] = abs(x1 - x2) + abs(y1 - y2) + dis[i - d]
res = []
q = int(input())
for i in range(q):
	(l, r) = list(map(int, input().split()))
	res.append(dis[r] - dis[l])
print('\n'.join(map(str, res)))


(h, w, d) = map(int, input().split())
px = [0] * (h * w + 1)
py = [0] * (h * w + 1)
for i in range(h):
	l = list(map(int, input().split()))
	for j in range(w):
		(px[l[j]], py[l[j]]) = (i, j)
count = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	count[i] = count[i - d] + abs(px[i] - px[i - d]) + abs(py[i] - py[i - d])
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	print(count[b] - count[a])


(h, w, d) = map(int, input().split())
xy = [(0, 0)] * (h * w + 1)
for i in range(h):
	a = list(map(int, input().split()))
	for j in range(w):
		xy[a[j]] = (i + 1, j + 1)
s = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	s[i] = abs(xy[i][0] - xy[i - d][0]) + abs(xy[i][1] - xy[i - d][1]) + s[i - d]
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(s[r] - s[l])


(h, w, d) = map(int, input().split())
A = []
for i in range(h):
	A.append(list(map(int, input().split())))
q = int(input())
I = [0] * (w * h + 1)
for i in range(h):
	for j in range(w):
		I[A[i][j]] = (i, j)
D = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	D[i] = D[i - d] + abs(I[i][0] - I[i - d][0]) + abs(I[i][1] - I[i - d][1])
s = []
g = []
for i in range(q):
	(s, g) = map(int, input().split())
	print(D[g] - D[s])


(h, w, d) = map(int, input().split())
a = [0] * h
for i in range(h):
	a[i] = list(map(int, input().split()))
num = [[] for i in range(w * h + 1)]
for i in range(h):
	for j in range(w):
		num[a[i][j]].append([i, j])
s = [0] * (h * w + 1)
for i in range(1, h * w - d + 1):
	s[d + i] = s[i] + abs(num[d + i][0][0] - num[i][0][0]) + abs(num[d + i][0][1] - num[i][0][1])
q = int(input())
for i in range(q):
	(L, R) = map(int, input().split())
	print(s[R] - s[L])


(H, W, D) = map(int, input().split())
x = [0] * (H * W + 1)
for i in range(H):
	a = list(map(int, input().split()))
	for j in range(W):
		x[a[j]] = [i, j]
d = [0] * (H * W + 1)
for i in range(1, H * W + 1):
	if i > D:
		d[i] = d[i - D] + abs(x[i][0] - x[i - D][0]) + abs(x[i][1] - x[i - D][1])
Q = int(input())
for i in range(Q):
	(l, r) = map(int, input().split())
	print(d[r] - d[l])


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
place = [(0, 0) for _ in range(h * w + 1)]
for r in range(h):
	for c in range(w):
		place[a[r][c]] = (r, c)
dp = [0] * (w * h + 1)
for r in range(1, w * h + 1):
	if r - d < 1:
		continue
	(r1, c1) = place[r - d]
	(r2, c2) = place[r]
	dp[r] = dp[r - d] + abs(r1 - r2) + abs(c1 - c2)
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(dp[r] - dp[l])


def main():
	(h, w, d) = list(map(int, input().split()))
	a = [None] * (h * w + 1)
	for i in range(1, h + 1):
		l = list(map(int, input().split()))
		for (j, k) in enumerate(l, 1):
			a[k] = (i, j)
	c = [0] * (h * w + 1)
	for i in range(d + 1, h * w + 1):
		(xb, yb) = a[i - d]
		(xn, yn) = a[i]
		c[i] = c[i - d] + abs(xn - xb) + abs(yn - yb)
	q = int(input())
	res = []
	for _ in range(q):
		(l, r) = list(map(int, input().split()))
		res.append(c[r] - c[l])
	ans = '\n'.join(map(str, res))
	print(ans)

def __starting_point():
	main()
__starting_point()


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Am = [None] * (H * W + 1)
for h in range(H):
	for w in range(W):
		Am[A[h][w]] = (h, w)
C = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	(h, w) = Am[i - D]
	(ch, cw) = Am[i]
	C[i] = C[i - D] + abs(ch - h) + abs(cw - w)
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(C[R] - C[L])


(H, W, D) = map(int, input().split())
xy = {}
for i in range(H):
	A = list(map(int, input().split()))
	for j in range(W):
		xy[A[j]] = (i + 1, j + 1)
d = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	(x1, y1) = xy[i]
	(x2, y2) = xy[i - D]
	d[i] = d[i - D] + abs(x1 - x2) + abs(y1 - y2)
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	ans = d[R] - d[L]
	print(ans)


(H, W, D) = map(int, input().split())
A = [()] * (H * W + 1)
for i in range(H):
	for (j, a) in enumerate(map(int, input().split())):
		A[a] = (i, j)
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]

def distance(a, b):
	return abs(a[0] - b[0]) + abs(a[1] - b[1])
dist = [0] * (H * W + 1)
for o in range(1, D + 1):
	for i in range(o + D, H * W + 1, D):
		dist[i] = dist[i - D] + distance(A[i], A[i - D])
for (L, R) in LR:
	print(dist[R] - dist[L])


def dist(d1, d2):
	return abs(d1[0] - d2[0]) + abs(d1[1] - d2[1])
(h, w, d) = map(int, input().split())
table = {}
for i in range(h):
	a = list(map(int, input().split()))
	for j in range(w):
		table[a[j]] = (j + 1, i + 1)
cum = [0] * (h * w + 1)
for i in range(h * w + 1):
	if i > d:
		cum[i] = cum[i - d] + dist(table[i], table[i - d])
q = int(input())
for i in range(q):
	(l, r) = map(int, input().split())
	print(cum[r] - cum[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]
data = {A[i][j]: j + i * W for i in range(H) for j in range(W)}
memo = [0] * (H * W + 1)
for i in range(1 + D, H * W + 1):
	(a, b) = (data[i], data[i - D])
	memo[i] = memo[i - D] + abs(a % W - b % W) + abs(a // W - b // W)
for (l, r) in LR:
	if l == r:
		print(0)
		continue
	print(memo[r] - memo[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
dic = {}
for i in range(H):
	for j in range(W):
		dic[A[i][j]] = (i, j)
dist = [0] * (H * W + 10)
for i in range(D + 1, H * W + 1):
	xd = abs(dic[i][0] - dic[i - D][0])
	yd = abs(dic[i][1] - dic[i - D][1])
	dist[i] = dist[i - D] + xd + yd
Q = int(input())
for i in range(Q):
	(L, R) = map(int, input().split())
	print(dist[R] - dist[L])


import sys
readline = sys.stdin.buffer.readline
(H, W, D) = map(int, readline().split())
px = [0] * (H * W + 1)
py = [0] * (H * W + 1)
d = [0] * (H * W + 1)
for y in range(H):
	line = list(map(int, readline().split()))
	for (x, A) in enumerate(line):
		px[A] = x
		py[A] = y
for i in range(D + 1, H * W + 1):
	d[i] = d[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(d[R] - d[L])


(h, w, d) = map(int, input().split())
zahyo = [0] * (h * w + 1)
for i in range(h):
	a = list(map(int, input().split()))
	for j in range(w):
		zahyo[a[j]] = (i + 1, j + 1)
D = [0] * 100000
for i in range(d + 1, h * w + 1):
	D[i] = D[i - d] + abs(zahyo[i][1] - zahyo[i - d][1]) + abs(zahyo[i][0] - zahyo[i - d][0])
q = int(input())
for i in range(q):
	(L, R) = map(int, input().split())
	print(D[R] - D[L])


import sys
input = sys.stdin.readline
(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
loc = [None] * (H * W + 1)
for y in range(H):
	for x in range(W):
		loc[A[y][x]] = (x, y)
power = [0] * (H * W + 1)
for n in range(D + 1, H * W + 1):
	(i, j) = loc[n - D]
	(x, y) = loc[n]
	power[n] = power[n - D] + abs(x - i) + abs(y - j)
Q = int(input())
for _ in range(Q):
	(L, R) = map(int, input().split())
	print(power[R] - power[L])


(H, W, D) = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(H)]
L = [[] for i in range(H * W + 1)]
for i in range(H):
	for j in range(W):
		L[A[i][j]] = [i + 1, j + 1]
d = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	d[i] = d[i - D] + abs(L[i][0] - L[i - D][0]) + abs(L[i][1] - L[i - D][1])
Q = int(input())
for i in range(Q):
	(l, r) = map(int, input().split())
	print(d[r] - d[l])


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
q = int(input())
lr = [list(map(int, input().split())) for _ in range(q)]
b = [0] * (h * w + 1)
for i in range(h):
	for j in range(w):
		b[a[i][j]] = (i, j)
ans = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
	ans[i] = ans[i - d] + abs(b[i][0] - b[i - d][0]) + abs(b[i][1] - b[i - d][1])
for i in range(q):
	(x, y) = (lr[i][0], lr[i][1])
	print(ans[y] - ans[x])


(H, W, D) = map(int, input().split())
dic = {}
memo = [0] * (H * W + 1)
for i in range(H):
	s = input().split()
	for j in range(W):
		dic[int(s[j])] = (i, j)
for i in range(1, H * W + 1):
	if i - D >= 1:
		memo[i] = memo[i - D] + abs(dic[i][0] - dic[i - D][0]) + abs(dic[i][1] - dic[i - D][1])
Q = int(input())
for _ in range(Q):
	(l, r) = map(int, input().split())
	print(memo[r] - memo[l])


import sys
readline = sys.stdin.readline
(H, W, D) = list(map(int, readline().split()))
route = [0] * (H * W + 1)
for i in range(H):
	line = list(map(int, readline().split()))
	for j in range(len(line)):
		route[line[j]] = (i, j)
dist = [0] * (H * W + 1)
for i in range(D + 1, len(route)):
	(gy, gx) = route[i]
	(sy, sx) = route[i - D]
	dist[i] = abs(gy - sy) + abs(gx - sx)
	dist[i] = dist[i] + dist[i - D]
Q = int(readline())
for i in range(Q):
	(l, r) = list(map(int, readline().split()))
	print(dist[r] - dist[l])


(h, w, d) = map(int, input().split())
amd = {}
for i in range(h):
	al = map(int, input().split())
	for (j, a) in enumerate(al):
		amd[a] = (j + 1, i + 1)
dsum = [0 for _ in range(d + 1)]
for i in range(d + 1, h * w + 1):
	dsum.append(dsum[-d] + abs(amd[i][0] - amd[i - d][0]) + abs(amd[i][1] - amd[i - d][1]))
q = int(input())
for _ in range(q):
	ans = 0
	(l, r) = map(int, input().split())
	print(dsum[r] - dsum[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
(L, R) = zip(*(map(int, input().split()) for _ in range(Q)))
p = {A[i][j]: (i, j) for i in range(H) for j in range(W)}
dp = [0 for _ in range(H * W + 1)]
for x in range(D + 1, H * W + 1):
	y = x - D
	dp[x] = dp[y] + abs(p[x][0] - p[y][0]) + abs(p[x][1] - p[y][1])
ans = '\n'.join((str(dp[r] - dp[l]) for (l, r) in zip(L, R)))
print(ans)


(H, W, D) = map(int, input().split())
A = []
for i in range(H):
	A.append(list(map(int, input().split())))
num = [() for _ in range(H * W + 1)]
for i in range(H):
	for j in range(W):
		num[A[i][j]] = (i, j)
wa = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	wa[i] = wa[i - D] + abs(num[i][0] - num[i - D][0]) + abs(num[i][1] - num[i - D][1])
Q = int(input())
for i in range(Q):
	(l, r) = map(int, input().split())
	print(wa[r] - wa[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
X = [list(map(int, input().split())) for _ in range(Q)]
ctr = {}
for i in range(H):
	for j in range(W):
		ctr[A[i][j]] = (i, j)
memo = [0] * (H * W + 1)
for n in range(D + 1, H * W + 1):
	(i, j) = ctr[n - D]
	(x, y) = ctr[n]
	memo[n] = abs(x - i) + abs(y - j) + memo[n - D]
for (l, r) in X:
	print(memo[r] - memo[l])


(H, W, D) = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
	l = map(int, input().split())
	for (j, a) in enumerate(l):
		m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
	(l, r) = map(int, input().split())
	LR.append((l, r))
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
	(x, y) = m[k]
	(i, j) = m[k - D]
	S[k] = S[k - D] + abs(x - i) + abs(y - j)
for (l, r) in LR:
	print(S[r] - S[l])


(H, W, D) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
As = [(0, 0, 0)]
for i in range(H):
	for j in range(W):
		As.append((A[i][j], i + 1, j + 1))
Q = int(input())
Qs = [list(map(int, input().split())) for _ in range(Q)]
As.sort()
dp = [0] * (H * W + 1)
for i in range(1, H * W + 1 - D):
	(a1, j, k) = As[i]
	(a2, x, y) = As[i + D]
	dp[i + D] = dp[i] + abs(x - j) + abs(y - k)
for (l, r) in Qs:
	print(dp[r] - dp[l])


(h, w, d) = map(int, input().split())
index = [(0, 0) for x in range(h * w + 1)]
for i in range(1, h + 1):
	col = list(map(int, input().split()))
	for j in range(1, w + 1):
		index[col[j - 1]] = (i, j)
memo = [0 for x in range(h * w + 1)]
l = 0
for l in range(1, h * w + 1 - d):
	memo[l + d] = memo[l] + abs(index[l + d][0] - index[l][0]) + abs(index[l + d][1] - index[l][1])
q = int(input())
for i in range(q):
	(l, r) = map(int, input().split())
	print(memo[r] - memo[l])


(h, w, d) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
dic = {}
for i in range(h):
	for j in range(w):
		dic[a[i][j]] = (i, j)
cnt = [0] * (h * w + 1)
for j in range(1, d + 1):
	for i in range(j, h * w - d + 1, d):
		(y1, x1) = dic[i + d]
		(y2, x2) = dic[i]
		cnt[i + d] = cnt[i] + abs(x2 - x1) + abs(y2 - y1)
q = int(input())
for _ in range(q):
	(l, r) = map(int, input().split())
	print(cnt[r] - cnt[l])


(H, W, D) = map(int, input().split())
l = [None] * (H * W + 1)
for i in range(H):
	s = tuple(map(int, input().split()))
	for j in range(W):
		l[s[j]] = (i, j)
S = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
	S[i] = S[i - D] + abs(l[i][0] - l[i - D][0]) + abs(l[i][1] - l[i - D][1])
Q = int(input())
for i in range(Q):
	(L, R) = map(int, input().split())
	print(S[R] - S[L])


ai = lambda : list(map(int, input().split()))
(h, w, d) = ai()
ph = [0 for _ in range(h * w + 1)]
pw = [0 for _ in range(h * w + 1)]
for i in range(h):
	a = ai()
	for j in range(w):
		ph[a[j]] = i
		pw[a[j]] = j
score = [0 for _ in range(h * w + 1)]
for i in range(d + 1, h * w + 1):
	score[i] = score[i - d] + abs(ph[i] - ph[i - d]) + abs(pw[i] - pw[i - d])
q = int(input())
for _ in range(q):
	(l, r) = ai()
	print(score[r] - score[l])