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

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

Q = int(eval(input()))

qs = [tuple(map(int,input().split())) for i in range(Q)]



pos = [[None for i in range(D)] for j in range(H*W//D + 1)]

for y in range(H):

    for x in range(W):

        a = src[y][x]

        b,c = a//D, a%D

        pos[b][c] = (x,y)



cums = [[0] for i in range(D)]

for i in range(D):

    for j in range(H*W//D):

        if pos[j][i] is None or pos[j+1][i] is None:

            cums[i].append(cums[i][-1])

        else:

            x1,y1 = pos[j][i]

            x2,y2 = pos[j+1][i]

            dist = abs(x1-x2) + abs(y1-y2)

            cums[i].append(cums[i][-1] + dist)

            

for l,r in qs:

    d = l%D

    print((cums[d][r//D] - cums[d][l//D]))

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

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

Q = int(input())

qs = [tuple(map(int,input().split())) for i in range(Q)]



places = [None] * (H*W+1)

for i in range(H):

    for j in range(W):

        places[A[i][j]] = (j,i)



costs = [[] for i in range(D)]

for i in range(H*W+1):

    if i <= D:

        costs[i%D].append(0)

    else:

        x1,y1 = places[i-D]

        x2,y2 = places[i]

        c = abs(x1-x2) + abs(y1-y2)

        costs[i%D].append(costs[i%D][-1] + c)



ans = []

for l,r in qs:

    m = l%D

    a = costs[m][r//D] - costs[m][l//D]

    ans.append(a)

print(*ans, sep='\n')

H, W, D = list(map(int, input().split()))
src = [list(map(int, input().split())) for i in range(H)]
Q = int(eval(input()))
qs = [tuple(map(int, input().split())) for i in range(Q)]
pos = [[None for i in range(D)] for j in range(H * W // D + 1)]
for y in range(H):
    for x in range(W):
        a = src[y][x]
        b, c = a // D, a % D
        pos[b][c] = (x, y)
cums = [[0] for i in range(D)]
for i in range(D):
    for j in range(H * W // D):
        if pos[j][i] is None or pos[j + 1][i] is None:
            cums[i].append(cums[i][-1])
        else:
            x1, y1 = pos[j][i]
            x2, y2 = pos[j + 1][i]
            dist = abs(x1 - x2) + abs(y1 - y2)
            cums[i].append(cums[i][-1] + dist)
for l, r in qs:
    d = l % D
    print((cums[d][r // D] - cums[d][l // D]))

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
Q = int(input())
qs = [tuple(map(int, input().split())) for i in range(Q)]
places = [None] * (H * W + 1)
for i in range(H):
    for j in range(W):
        places[A[i][j]] = (j, i)
costs = [[] for i in range(D)]
for i in range(H * W + 1):
    if i <= D:
        costs[i % D].append(0)
    else:
        x1, y1 = places[i - D]
        x2, y2 = places[i]
        c = abs(x1 - x2) + abs(y1 - y2)
        costs[i % D].append(costs[i % D][-1] + c)
ans = []
for l, r in qs:
    m = l % D
    a = costs[m][r // D] - costs[m][l // D]
    ans.append(a)
print(*ans, sep="\n")

-H, W, D = list(map(int, input().split())) -src = [list(map(int, input().split())) for i in range(H)] -Q = int(eval(input())) +H, W, D = map(int, input().split()) +A = [list(map(int, input().split())) for i in range(H)] +Q = int(input()) -pos = [[None for i in range(D)] for j in range(H * W // D + 1)] -for y in range(H): -    for x in range(W): -        a = src[y][x] -        b, c = a // D, a % D -        pos[b][c] = (x, y) -cums = [[0] for i in range(D)] -for i in range(D): -    for j in range(H * W // D): -        if pos[j][i] is None or pos[j + 1][i] is None: -            cums[i].append(cums[i][-1]) -        else: -            x1, y1 = pos[j][i] -            x2, y2 = pos[j + 1][i] -            dist = abs(x1 - x2) + abs(y1 - y2) -            cums[i].append(cums[i][-1] + dist) +places = [None] * (H * W + 1) +for i in range(H): +    for j in range(W): +        places[A[i][j]] = (j, i) +costs = [[] for i in range(D)] +for i in range(H * W + 1): +    if i <= D: +        costs[i % D].append(0) +    else: +        x1, y1 = places[i - D] +        x2, y2 = places[i] +        c = abs(x1 - x2) + abs(y1 - y2) +        costs[i % D].append(costs[i % D][-1] + c) +ans = [] -    d = l % D -    print((cums[d][r // D] - cums[d][l // D])) +    m = l % D +    a = costs[m][r // D] - costs[m][l // D] +    ans.append(a) +print(*ans, sep="\n")