# ABC089D - Practical Skill Test

def main():

    H, W, D, *X = list(map(int, open(0).read().split()))

    memo = {}  # memo[i] := coordinate of i

    for i, a in enumerate(zip(*[iter(X[: H * W])] * W), 1):

        for j, x in enumerate(a, 1):

            memo[x] = (i, j)

    dist = [0] * (H * W + 1)  # dist[i] := distance to i from 1

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

        rx, ry = memo[r]

        l = r - D

        lx, ly = memo[r - D]

        dist[r] = dist[l] + abs(rx - lx) + abs(ry - ly)

    ans = []

    for l, r in zip(*[iter(X[H * W + 1 :])] * 2):

        ans.append(dist[r] - dist[l])

    print(("\n".join(map(str, ans))))





if __name__ == "__main__":

    main()
# ABC089D - Practical Skill Test

def main():

    H, W, D, *X = list(map(int, open(0).read().split()))

    memo = [None] * (H * W + 1)  # memo[i] := coordinate of i

    for i, a in enumerate(zip(*[iter(X[: H * W])] * W), 1):

        for j, x in enumerate(a, 1):

            memo[x] = (i, j)

    dist = [0] * (H * W + 1)  # dist[i] := distance to i from 1

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

        rx, ry = memo[r]

        l = r - D

        lx, ly = memo[r - D]

        dist[r] = dist[l] + abs(rx - lx) + abs(ry - ly)

    ans = []

    for l, r in zip(*[iter(X[H * W + 1 :])] * 2):

        ans.append(dist[r] - dist[l])

    print(("\n".join(map(str, ans))))





if __name__ == "__main__":

    main()
# ABC089D - Practical Skill Test
def main():
    H, W, D, *X = list(map(int, open(0).read().split()))
    memo = {}  # memo[i] := coordinate of i
    for i, a in enumerate(zip(*[iter(X[: H * W])] * W), 1):
        for j, x in enumerate(a, 1):
            memo[x] = (i, j)
    dist = [0] * (H * W + 1)  # dist[i] := distance to i from 1
    for r in range(D + 1, H * W + 1):
        rx, ry = memo[r]
        l = r - D
        lx, ly = memo[r - D]
        dist[r] = dist[l] + abs(rx - lx) + abs(ry - ly)
    ans = []
    for l, r in zip(*[iter(X[H * W + 1 :])] * 2):
        ans.append(dist[r] - dist[l])
    print(("\n".join(map(str, ans))))


if __name__ == "__main__":
    main()

# ABC089D - Practical Skill Test
def main():
    H, W, D, *X = list(map(int, open(0).read().split()))
    memo = [None] * (H * W + 1)  # memo[i] := coordinate of i
    for i, a in enumerate(zip(*[iter(X[: H * W])] * W), 1):
        for j, x in enumerate(a, 1):
            memo[x] = (i, j)
    dist = [0] * (H * W + 1)  # dist[i] := distance to i from 1
    for r in range(D + 1, H * W + 1):
        rx, ry = memo[r]
        l = r - D
        lx, ly = memo[r - D]
        dist[r] = dist[l] + abs(rx - lx) + abs(ry - ly)
    ans = []
    for l, r in zip(*[iter(X[H * W + 1 :])] * 2):
        ans.append(dist[r] - dist[l])
    print(("\n".join(map(str, ans))))


if __name__ == "__main__":
    main()

-    memo = {}  # memo[i] := coordinate of i +    memo = [None] * (H * W + 1)  # memo[i] := coordinate of i