def main():

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

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



    c = [0] * (H * W)

    for i in range(H):

        for j in range(W):

            c[field[i][j] - 1] = (i, j)

    

    d = [0] * (H * W)

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

        i, j = c[l]

        x, y = c[l + D]

        d[l+D] = d[l] + abs(x - i) + abs(y - j)

    

    Q = int(eval(input()))

    for _ in range(Q):

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

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





main()
import sys

input = sys.stdin.readline



def main():

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

    A = list(list(map(int, input().split())) for _ in range(H))

    Q = int(eval(input()))

    B = [(0,0)] * (H * W + 5)

    for h in range(H):

        for w in range(W): 

            B[A[h][w]] = (h+1, w+1)





    C = [0] * (H * W + 5)

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

        if i // D == 0:

            continue

        tmp = abs(B[i][0] - B[i-D][0]) + abs(B[i][1] - B[i-D][1]) 

        C[i] = tmp + C[i - D]



    for _ in range(Q):

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

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





if __name__ == "__main__":

    main()
def main():
    H, W, D = list(map(int, input().split()))
    field = [list(map(int, input().split())) for _ in range(H)]
    c = [0] * (H * W)
    for i in range(H):
        for j in range(W):
            c[field[i][j] - 1] = (i, j)
    d = [0] * (H * W)
    for l in range(H * W - D):
        i, j = c[l]
        x, y = c[l + D]
        d[l + D] = d[l] + abs(x - i) + abs(y - j)
    Q = int(eval(input()))
    for _ in range(Q):
        L, R = list(map(int, input().split()))
        print((d[R - 1] - d[L - 1]))


main()

import sys

input = sys.stdin.readline


def main():
    H, W, D = list(map(int, input().split()))
    A = list(list(map(int, input().split())) for _ in range(H))
    Q = int(eval(input()))
    B = [(0, 0)] * (H * W + 5)
    for h in range(H):
        for w in range(W):
            B[A[h][w]] = (h + 1, w + 1)
    C = [0] * (H * W + 5)
    for i in range(H * W + 1):
        if i // D == 0:
            continue
        tmp = abs(B[i][0] - B[i - D][0]) + abs(B[i][1] - B[i - D][1])
        C[i] = tmp + C[i - D]
    for _ in range(Q):
        L, R = list(map(int, input().split()))
        print((C[R] - C[L]))


if __name__ == "__main__":
    main()

+import sys + +input = sys.stdin.readline + + -    field = [list(map(int, input().split())) for _ in range(H)] -    c = [0] * (H * W) -    for i in range(H): -        for j in range(W): -            c[field[i][j] - 1] = (i, j) -    d = [0] * (H * W) -    for l in range(H * W - D): -        i, j = c[l] -        x, y = c[l + D] -        d[l + D] = d[l] + abs(x - i) + abs(y - j) +    A = list(list(map(int, input().split())) for _ in range(H)) +    B = [(0, 0)] * (H * W + 5) +    for h in range(H): +        for w in range(W): +            B[A[h][w]] = (h + 1, w + 1) +    C = [0] * (H * W + 5) +    for i in range(H * W + 1): +        if i // D == 0: +            continue +        tmp = abs(B[i][0] - B[i - D][0]) + abs(B[i][1] - B[i - D][1]) +        C[i] = tmp + C[i - D] -        print((d[R - 1] - d[L - 1])) +        print((C[R] - C[L])) -main() +if __name__ == "__main__": +    main()