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

grid_dict = {}

for i in range(H):

    for j,s in enumerate(input().split()):

        grid_dict[int(s)] = [i,j]



Q = int(eval(input()))

exam = []

for i in range(Q):

    exam.append(list(map(int, input().split())))



man = lambda x,y: abs(x[0]-y[0])+abs(x[1]-y[1])



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

power[0].append(0)

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

    if i + D not in grid_dict:

        break

    power[i%D].append(power[i%D][-1] + man(grid_dict[i], grid_dict[i+D]))

# [print(i) for i in power]



for l,r in exam:

    if r == l:

        print((0))

        continue

    print((power[r%D][r//D]-power[r%D][l//D]))

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

A = {}

for i in range(H):

    for j, s in enumerate(input().split()):

        A[int(s)] = [i,j]

Q = int(eval(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]))

H, W, D = list(map(int, input().split()))
grid_dict = {}
for i in range(H):
    for j, s in enumerate(input().split()):
        grid_dict[int(s)] = [i, j]
Q = int(eval(input()))
exam = []
for i in range(Q):
    exam.append(list(map(int, input().split())))
man = lambda x, y: abs(x[0] - y[0]) + abs(x[1] - y[1])
power = [[0] for i in range(D)]
power[0].append(0)
for i in range(1, H * W + 1):
    if i + D not in grid_dict:
        break
    power[i % D].append(power[i % D][-1] + man(grid_dict[i], grid_dict[i + D]))
# [print(i) for i in power]
for l, r in exam:
    if r == l:
        print((0))
        continue
    print((power[r % D][r // D] - power[r % D][l // D]))

H, W, D = list(map(int, input().split()))
A = {}
for i in range(H):
    for j, s in enumerate(input().split()):
        A[int(s)] = [i, j]
Q = int(eval(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]))

-grid_dict = {} +A = {} -        grid_dict[int(s)] = [i, j] +        A[int(s)] = [i, j] -man = lambda x, y: abs(x[0] - y[0]) + abs(x[1] - y[1]) -power = [[0] for i in range(D)] -power[0].append(0) +grid = [0] +manta = lambda x, y: abs(x[0] - y[0]) + abs(x[1] - y[1]) -    if i + D not in grid_dict: -        break -    power[i % D].append(power[i % D][-1] + man(grid_dict[i], grid_dict[i + D])) -# [print(i) for i in power] +    if (i - 1) // D == 0: +        grid.append(0) +    else: +        grid.append(manta(A[i], A[i - D]) + grid[i - D]) -    if r == l: -        print((0)) -        continue -    print((power[r % D][r // D] - power[r % D][l // D])) +    print((grid[r] - grid[l]))