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

A = [[int(j) for j in input().split()] for i in range(H)]

B = [(-1, -1) for j in range(H*W)]

for i in range(H):

    for j in range(W):

        B[A[i][j]-1] = (i, j)



C = [-1] * H*W

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

    fx, fy = B[i+D]

    tx, ty = B[i]

    C[i] = abs(fx-tx)+abs(fy-ty)



E = [0] * H*W

for i in range(D):

    S = 0

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

        S += C[j]

        E[j+D] = S

# print(E)



Q = int(eval(input()))

for _ in range(Q):

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

    print((E[R-1]-E[L-1]))

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

A = [[int(i) for i in input().split()] for _ in range(H)]

Q = int(eval(input()))

query = [(int(i) for i in input().split()) for _ in range(Q)]



x = [-1]*H*W

y = [-1]*H*W



for i in range(H):

    for j in range(W):

        x[A[i][j]-1] = i

        y[A[i][j]-1] = j



X = [0]*H*W

Y = [0]*H*W

# 累積和行列化

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

    X[k] = X[k-D]+abs(x[k]-x[k-D])

    Y[k] = Y[k-D]+abs(y[k]-y[k-D])



for L, R in query:

    L -= 1  # 0-indexed

    R -= 1  # 0-indexed

    print((X[R]-X[L]+Y[R]-Y[L]))

H, W, D = list(map(int, input().split()))
A = [[int(j) for j in input().split()] for i in range(H)]
B = [(-1, -1) for j in range(H * W)]
for i in range(H):
    for j in range(W):
        B[A[i][j] - 1] = (i, j)
C = [-1] * H * W
for i in range(H * W - D):
    fx, fy = B[i + D]
    tx, ty = B[i]
    C[i] = abs(fx - tx) + abs(fy - ty)
E = [0] * H * W
for i in range(D):
    S = 0
    for j in range(i, H * W - D, D):
        S += C[j]
        E[j + D] = S
# print(E)
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    print((E[R - 1] - E[L - 1]))

H, W, D = list(map(int, input().split()))
A = [[int(i) for i in input().split()] for _ in range(H)]
Q = int(eval(input()))
query = [(int(i) for i in input().split()) for _ in range(Q)]
x = [-1] * H * W
y = [-1] * H * W
for i in range(H):
    for j in range(W):
        x[A[i][j] - 1] = i
        y[A[i][j] - 1] = j
X = [0] * H * W
Y = [0] * H * W
# 累積和行列化
for k in range(D, H * W):
    X[k] = X[k - D] + abs(x[k] - x[k - D])
    Y[k] = Y[k - D] + abs(y[k] - y[k - D])
for L, R in query:
    L -= 1  # 0-indexed
    R -= 1  # 0-indexed
    print((X[R] - X[L] + Y[R] - Y[L]))

-A = [[int(j) for j in input().split()] for i in range(H)] -B = [(-1, -1) for j in range(H * W)] +A = [[int(i) for i in input().split()] for _ in range(H)] +Q = int(eval(input())) +query = [(int(i) for i in input().split()) for _ in range(Q)] +x = [-1] * H * W +y = [-1] * H * W -        B[A[i][j] - 1] = (i, j) -C = [-1] * H * W -for i in range(H * W - D): -    fx, fy = B[i + D] -    tx, ty = B[i] -    C[i] = abs(fx - tx) + abs(fy - ty) -E = [0] * H * W -for i in range(D): -    S = 0 -    for j in range(i, H * W - D, D): -        S += C[j] -        E[j + D] = S -# print(E) -Q = int(eval(input())) -for _ in range(Q): -    L, R = list(map(int, input().split())) -    print((E[R - 1] - E[L - 1])) +        x[A[i][j] - 1] = i +        y[A[i][j] - 1] = j +X = [0] * H * W +Y = [0] * H * W +# 累積和行列化 +for k in range(D, H * W): +    X[k] = X[k - D] + abs(x[k] - x[k - D]) +    Y[k] = Y[k - D] + abs(y[k] - y[k - D]) +for L, R in query: +    L -= 1  # 0-indexed +    R -= 1  # 0-indexed +    print((X[R] - X[L] + Y[R] - Y[L]))