H, W, D = list(map(int, input().split()))



#盤面を取得

lst = [0] * (H * W + 1)

for i in range(1, H + 1):

    tmp = list(map(int, input().split()))

    for j in range(1, W + 1):

        lst[tmp[j - 1]] = [i, j]

#前処理



#xからyに移動するのにかかるコストを返す関数

def cost(x, y): 

    return abs(lst[x][0] - lst[y][0]) + abs(lst[x][1] - lst[y][1])



check_lst = [False] * (H * W + 1)

cost_lst = [0] * (H * W + 1)

for i in range(1, D + 1):

    if check_lst[i]:

        continue

    check_lst[i]  = True

    tmp = i

    while tmp + D <= H * W:

        cost_lst[tmp + D] = cost_lst[tmp] + cost(tmp, tmp + D)

        check_lst[tmp + D] = True

        tmp += D



#出力

Q = int(eval(input()))

for _ in range(Q):

    L, R = list(map(int, input().split()))

    print((cost_lst[R] - cost_lst[L]))

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)

MOD = 10 ** 9 + 7



from collections import defaultdict



H, W, D = list(map(int, input().split()))

A = [list(map(int, input().split()))  for _ in range(H)]



dic = defaultdict(list)



for h in range(H):

    for w in range(W):

        dic[A[h][w]] = [h, w]



def calc(l, r):

    return abs(dic[l][0] - dic[r][0]) + abs(dic[l][1] - dic[r][1])



lst = [0] * (H * W + 1)

for d in range(1, D + 1):

    tmp = d

    while tmp + D <= (H * W):

        lst[tmp + D] = lst[tmp] + calc(tmp, tmp + D)

        tmp += D



# print (lst)

Q = int(eval(input()))

for _ in range(Q):

    L, R = list(map(int, input().split()))

    ans = lst[R] - lst[L]

    print (ans)

H, W, D = list(map(int, input().split()))
# 盤面を取得
lst = [0] * (H * W + 1)
for i in range(1, H + 1):
    tmp = list(map(int, input().split()))
    for j in range(1, W + 1):
        lst[tmp[j - 1]] = [i, j]
# 前処理
# xからyに移動するのにかかるコストを返す関数
def cost(x, y):
    return abs(lst[x][0] - lst[y][0]) + abs(lst[x][1] - lst[y][1])


check_lst = [False] * (H * W + 1)
cost_lst = [0] * (H * W + 1)
for i in range(1, D + 1):
    if check_lst[i]:
        continue
    check_lst[i] = True
    tmp = i
    while tmp + D <= H * W:
        cost_lst[tmp + D] = cost_lst[tmp] + cost(tmp, tmp + D)
        check_lst[tmp + D] = True
        tmp += D
# 出力
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    print((cost_lst[R] - cost_lst[L]))

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**9)
MOD = 10**9 + 7
from collections import defaultdict

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
dic = defaultdict(list)
for h in range(H):
    for w in range(W):
        dic[A[h][w]] = [h, w]


def calc(l, r):
    return abs(dic[l][0] - dic[r][0]) + abs(dic[l][1] - dic[r][1])


lst = [0] * (H * W + 1)
for d in range(1, D + 1):
    tmp = d
    while tmp + D <= (H * W):
        lst[tmp + D] = lst[tmp] + calc(tmp, tmp + D)
        tmp += D
# print (lst)
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    ans = lst[R] - lst[L]
    print(ans)

+import sys + +input = sys.stdin.readline +sys.setrecursionlimit(10**9) +MOD = 10**9 + 7 +from collections import defaultdict + -# 盤面を取得 -lst = [0] * (H * W + 1) -for i in range(1, H + 1): -    tmp = list(map(int, input().split())) -    for j in range(1, W + 1): -        lst[tmp[j - 1]] = [i, j] -# 前処理 -# xからyに移動するのにかかるコストを返す関数 -def cost(x, y): -    return abs(lst[x][0] - lst[y][0]) + abs(lst[x][1] - lst[y][1]) +A = [list(map(int, input().split())) for _ in range(H)] +dic = defaultdict(list) +for h in range(H): +    for w in range(W): +        dic[A[h][w]] = [h, w] -check_lst = [False] * (H * W + 1) -cost_lst = [0] * (H * W + 1) -for i in range(1, D + 1): -    if check_lst[i]: -        continue -    check_lst[i] = True -    tmp = i -    while tmp + D <= H * W: -        cost_lst[tmp + D] = cost_lst[tmp] + cost(tmp, tmp + D) -        check_lst[tmp + D] = True +def calc(l, r): +    return abs(dic[l][0] - dic[r][0]) + abs(dic[l][1] - dic[r][1]) + + +lst = [0] * (H * W + 1) +for d in range(1, D + 1): +    tmp = d +    while tmp + D <= (H * W): +        lst[tmp + D] = lst[tmp] + calc(tmp, tmp + D) -# 出力 +# print (lst) -    print((cost_lst[R] - cost_lst[L])) +    ans = lst[R] - lst[L] +    print(ans)