from itertools import accumulate



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

A = [[int(e)-1 for e in input().split()] for _ in range(H)]

Q = int(eval(input()))

query = [[int(e)-1 for e in input().split()] for _ in range(Q)]



pos = [-1]*(H*W)



for i, line in enumerate(A):

    for j, a in enumerate(line):

        pos[a] = (i,j)



mp_list = [[0] for _ in range(D)]



for r in range(D):

    res = []

    for k in range(H*W):

        if r + k*D <= H*W - 1:

            res.append(pos[r+k*D])

        else:

            break

    for l in range(len(res)-1):

        i, j = res[l]

        ni, nj = res[l+1]

        mp = abs(ni-i) + abs(nj-j)

        mp_list[r].append(mp)



for i in range(D):

    mp_list[i] = list(accumulate(mp_list[i]))



for left, right in query:

    r = left % D

    q1 = (left-r) // D

    q2 = (right-r) // D

    ans = mp_list[r][q2] - mp_list[r][q1]

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

A = [[int(e)-1 for e in input().split()] for _ in range(H)]

Q = int(eval(input()))

query = [[int(e)-1 for e in input().split()] for _ in range(Q)]



mp = [0]*(H*W)

x = [-1]*(H*W)

y = [-1]*(H*W)



for i, line in enumerate(A):

    for j, a in enumerate(line):

        x[a] = i

        y[a] = j



for i in range(D,H*W):

    mp[i] = mp[i-D] + abs(x[i]-x[i-D]) + abs(y[i]-y[i-D])



for l, r in query:

    print((mp[r]-mp[l]))
from itertools import accumulate

H, W, D = list(map(int, input().split()))
A = [[int(e) - 1 for e in input().split()] for _ in range(H)]
Q = int(eval(input()))
query = [[int(e) - 1 for e in input().split()] for _ in range(Q)]
pos = [-1] * (H * W)
for i, line in enumerate(A):
    for j, a in enumerate(line):
        pos[a] = (i, j)
mp_list = [[0] for _ in range(D)]
for r in range(D):
    res = []
    for k in range(H * W):
        if r + k * D <= H * W - 1:
            res.append(pos[r + k * D])
        else:
            break
    for l in range(len(res) - 1):
        i, j = res[l]
        ni, nj = res[l + 1]
        mp = abs(ni - i) + abs(nj - j)
        mp_list[r].append(mp)
for i in range(D):
    mp_list[i] = list(accumulate(mp_list[i]))
for left, right in query:
    r = left % D
    q1 = (left - r) // D
    q2 = (right - r) // D
    ans = mp_list[r][q2] - mp_list[r][q1]
    print(ans)

H, W, D = list(map(int, input().split()))
A = [[int(e) - 1 for e in input().split()] for _ in range(H)]
Q = int(eval(input()))
query = [[int(e) - 1 for e in input().split()] for _ in range(Q)]
mp = [0] * (H * W)
x = [-1] * (H * W)
y = [-1] * (H * W)
for i, line in enumerate(A):
    for j, a in enumerate(line):
        x[a] = i
        y[a] = j
for i in range(D, H * W):
    mp[i] = mp[i - D] + abs(x[i] - x[i - D]) + abs(y[i] - y[i - D])
for l, r in query:
    print((mp[r] - mp[l]))

-from itertools import accumulate - -pos = [-1] * (H * W) +mp = [0] * (H * W) +x = [-1] * (H * W) +y = [-1] * (H * W) -        pos[a] = (i, j) -mp_list = [[0] for _ in range(D)] -for r in range(D): -    res = [] -    for k in range(H * W): -        if r + k * D <= H * W - 1: -            res.append(pos[r + k * D]) -        else: -            break -    for l in range(len(res) - 1): -        i, j = res[l] -        ni, nj = res[l + 1] -        mp = abs(ni - i) + abs(nj - j) -        mp_list[r].append(mp) -for i in range(D): -    mp_list[i] = list(accumulate(mp_list[i])) -for left, right in query: -    r = left % D -    q1 = (left - r) // D -    q2 = (right - r) // D -    ans = mp_list[r][q2] - mp_list[r][q1] -    print(ans) +        x[a] = i +        y[a] = j +for i in range(D, H * W): +    mp[i] = mp[i - D] + abs(x[i] - x[i - D]) + abs(y[i] - y[i - D]) +for l, r in query: +    print((mp[r] - mp[l]))