

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



field = {}

for i in range(H):

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

    for j in range(W):

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



Q = int(eval(input()))





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

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

    cum[i] = cum[i-D] + abs(field[i][0] - field[i-D][0]) + abs(field[i][1] - field[i-D][1])



"""

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

    tmp = 0

    cum[i] = 0

    n = D

    while i+n <= H*W:

        cum[i+n] = cum[i+n-D] + abs(field[i+n][0] - field[i+n-D][0]) + abs(field[i+n][1] - field[i+n-D][1])

        #print("---", i+n, cum[i+n], field[i+n][0] , field[i+n-D][0], field[i+n][1] , field[i+n-D][1])

        n += D

"""

#print(cum)

for i in range(Q):

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

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



#print(field)


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



field = {}

for i in range(H):

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

    for j in range(W):

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



Q = int(eval(input()))





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

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

    cum[i] = cum[i-D] + abs(field[i][0] - field[i-D][0]) + abs(field[i][1] - field[i-D][1])



"""

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

    tmp = 0

    cum[i] = 0

    n = D

    while i+n <= H*W:

        cum[i+n] = cum[i+n-D] + abs(field[i+n][0] - field[i+n-D][0]) + abs(field[i+n][1] - field[i+n-D][1])

        #print("---", i+n, cum[i+n], field[i+n][0] , field[i+n-D][0], field[i+n][1] , field[i+n-D][1])

        n += D

"""

#print(cum)

LR = [None] * Q

for i in range(Q):

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

    LR[i] = (L,R)



for l,r in LR:

    print((cum[r] - cum[l]))



H, W, D = list(map(int, input().split()))
field = {}
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        field[A[j]] = (i, j)
Q = int(eval(input()))
cum = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
    cum[i] = (
        cum[i - D]
        + abs(field[i][0] - field[i - D][0])
        + abs(field[i][1] - field[i - D][1])
    )
"""
for i in range(1,D+1):
    tmp = 0
    cum[i] = 0
    n = D
    while i+n <= H*W:
        cum[i+n] = cum[i+n-D] + abs(field[i+n][0] - field[i+n-D][0]) + abs(field[i+n][1] - field[i+n-D][1])
        #print("---", i+n, cum[i+n], field[i+n][0] , field[i+n-D][0], field[i+n][1] , field[i+n-D][1])
        n += D
"""
# print(cum)
for i in range(Q):
    L, R = list(map(int, input().split()))
    print((cum[R] - cum[L]))
# print(field)

H, W, D = list(map(int, input().split()))
field = {}
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        field[A[j]] = (i, j)
Q = int(eval(input()))
cum = [0] * (H * W + 1)
for i in range(D + 1, H * W + 1):
    cum[i] = (
        cum[i - D]
        + abs(field[i][0] - field[i - D][0])
        + abs(field[i][1] - field[i - D][1])
    )
"""
for i in range(1,D+1):
    tmp = 0
    cum[i] = 0
    n = D
    while i+n <= H*W:
        cum[i+n] = cum[i+n-D] + abs(field[i+n][0] - field[i+n-D][0]) + abs(field[i+n][1] - field[i+n-D][1])
        #print("---", i+n, cum[i+n], field[i+n][0] , field[i+n-D][0], field[i+n][1] , field[i+n-D][1])
        n += D
"""
# print(cum)
LR = [None] * Q
for i in range(Q):
    L, R = list(map(int, input().split()))
    LR[i] = (L, R)
for l, r in LR:
    print((cum[r] - cum[l]))

+LR = [None] * Q -    print((cum[R] - cum[L])) -# print(field) +    LR[i] = (L, R) +for l, r in LR: +    print((cum[r] - cum[l]))