h, w, d = list(map(int, input().split()))

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



nc = dict()

for row in range(h):

    for col in range(w):

        v = A[row][col]

        nc[v] = (row, col)



q = int(eval(input()))

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



c_sums = [[0] for _ in range(d)]

for r in range(d):

    k = 0

    if r == 0: k = 1

    while r + (k + 1) * d <= h * w:

        i, j = nc[r + k * d]

        x, y = nc[r + (k + 1) * d]

        t = c_sums[r][-1] + abs(x - i) + abs(y - j)

        c_sums[r].append(t)

        k += 1



for lr in lrs:

    l, r = lr

    ld, rest = divmod(l, d)

    rd = r // d

    if rest == 0:

        ld -=1

        rd -=1

    print((c_sums[rest][rd] - c_sums[rest][ld]))
def main():

    import sys

    input = sys.stdin.readline



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

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



    B = [[] for _ in range(D)]



    C = [None] * (H * W)

    for r, row in enumerate(A):

        for c, cell in enumerate(row):

            C[cell - 1] = r, c



    for x in range(H * W):

        r = x % D

        if B[r]:

            cr, cc = C[x]

            pr, pc = C[x - D]

            cost = abs(cr - pr) + abs(cc - pc)

            B[r].append(B[r][-1] + cost)



        else:

            B[r].append(0)



    ans = []

    Q = int(input())

    for _ in range(Q):

        L, R = (int(x) - 1 for x in input().split())

        r = R % D

        ql, qr = L // D, R // D



        ans.append(B[r][qr] - B[r][ql])



    print(*ans, sep='\n')





if __name__ == '__main__':

    main()

h, w, d = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(h)]
nc = dict()
for row in range(h):
    for col in range(w):
        v = A[row][col]
        nc[v] = (row, col)
q = int(eval(input()))
lrs = [list(map(int, input().split())) for _ in range(q)]
c_sums = [[0] for _ in range(d)]
for r in range(d):
    k = 0
    if r == 0:
        k = 1
    while r + (k + 1) * d <= h * w:
        i, j = nc[r + k * d]
        x, y = nc[r + (k + 1) * d]
        t = c_sums[r][-1] + abs(x - i) + abs(y - j)
        c_sums[r].append(t)
        k += 1
for lr in lrs:
    l, r = lr
    ld, rest = divmod(l, d)
    rd = r // d
    if rest == 0:
        ld -= 1
        rd -= 1
    print((c_sums[rest][rd] - c_sums[rest][ld]))

def main():
    import sys

    input = sys.stdin.readline
    H, W, D = map(int, input().split())
    A = [[int(x) for x in input().split()] for _ in range(H)]
    B = [[] for _ in range(D)]
    C = [None] * (H * W)
    for r, row in enumerate(A):
        for c, cell in enumerate(row):
            C[cell - 1] = r, c
    for x in range(H * W):
        r = x % D
        if B[r]:
            cr, cc = C[x]
            pr, pc = C[x - D]
            cost = abs(cr - pr) + abs(cc - pc)
            B[r].append(B[r][-1] + cost)
        else:
            B[r].append(0)
    ans = []
    Q = int(input())
    for _ in range(Q):
        L, R = (int(x) - 1 for x in input().split())
        r = R % D
        ql, qr = L // D, R // D
        ans.append(B[r][qr] - B[r][ql])
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

-h, w, d = list(map(int, input().split())) -A = [list(map(int, input().split())) for _ in range(h)] -nc = dict() -for row in range(h): -    for col in range(w): -        v = A[row][col] -        nc[v] = (row, col) -q = int(eval(input())) -lrs = [list(map(int, input().split())) for _ in range(q)] -c_sums = [[0] for _ in range(d)] -for r in range(d): -    k = 0 -    if r == 0: -        k = 1 -    while r + (k + 1) * d <= h * w: -        i, j = nc[r + k * d] -        x, y = nc[r + (k + 1) * d] -        t = c_sums[r][-1] + abs(x - i) + abs(y - j) -        c_sums[r].append(t) -        k += 1 -for lr in lrs: -    l, r = lr -    ld, rest = divmod(l, d) -    rd = r // d -    if rest == 0: -        ld -= 1 -        rd -= 1 -    print((c_sums[rest][rd] - c_sums[rest][ld])) +def main(): +    import sys + +    input = sys.stdin.readline +    H, W, D = map(int, input().split()) +    A = [[int(x) for x in input().split()] for _ in range(H)] +    B = [[] for _ in range(D)] +    C = [None] * (H * W) +    for r, row in enumerate(A): +        for c, cell in enumerate(row): +            C[cell - 1] = r, c +    for x in range(H * W): +        r = x % D +        if B[r]: +            cr, cc = C[x] +            pr, pc = C[x - D] +            cost = abs(cr - pr) + abs(cc - pc) +            B[r].append(B[r][-1] + cost) +        else: +            B[r].append(0) +    ans = [] +    Q = int(input()) +    for _ in range(Q): +        L, R = (int(x) - 1 for x in input().split()) +        r = R % D +        ql, qr = L // D, R // D +        ans.append(B[r][qr] - B[r][ql]) +    print(*ans, sep="\n") + + +if __name__ == "__main__": +    main()