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

A = []



for _ in range(h):

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

    A.append(line)



num_to_xy = [[0, 0] for i in range(h*w + 1)]



for y in range(h):

    for x in range(w):

        num = A[y][x]

        num_to_xy[num] = [x, y]



costs = [[] for i in range(d+1)]

for mod in range(1, d+1):

    num = mod

    sx, sy = num_to_xy[num]

    cost = 0

    while num <= h*w:

        nx, ny = num_to_xy[num]

        cost += abs(nx-sx) + abs(ny-sy)

        costs[mod].append(cost)

        num += d

        sx, sy = nx, ny



q = int(eval(input()))



for _ in range(q):

    l, r = list(map(int, input().split()))

    mod = l % d

    if mod == 0:

        mod = d

    idx = (l-mod) // d

    step = (r-l) // d



    res = costs[mod][idx+step] - costs[mod][idx]

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

A = []



for _ in range(h):

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

    A.append(line)



num_to_xy = [[0, 0] for i in range(h*w + 1)]



for y in range(h):

    for x in range(w):

        num = A[y][x]

        num_to_xy[num] = [x, y]



costs = [0] * (h*w+1)



for start in range(1, d+1):

    cost = 0

    num = start

    sx, sy = num_to_xy[num]

    while num <= h*w:

        nx, ny = num_to_xy[num]

        cost += abs(nx-sx) + abs(ny-sy)

        costs[num] = cost

        sx, sy = nx, ny

        num += d



q = int(eval(input()))



for _ in range(q):

    l, r = list(map(int, input().split()))

    print((costs[r] - costs[l]))
h, w, d = list(map(int, input().split()))
A = []
for _ in range(h):
    line = list(map(int, input().split()))
    A.append(line)
num_to_xy = [[0, 0] for i in range(h * w + 1)]
for y in range(h):
    for x in range(w):
        num = A[y][x]
        num_to_xy[num] = [x, y]
costs = [[] for i in range(d + 1)]
for mod in range(1, d + 1):
    num = mod
    sx, sy = num_to_xy[num]
    cost = 0
    while num <= h * w:
        nx, ny = num_to_xy[num]
        cost += abs(nx - sx) + abs(ny - sy)
        costs[mod].append(cost)
        num += d
        sx, sy = nx, ny
q = int(eval(input()))
for _ in range(q):
    l, r = list(map(int, input().split()))
    mod = l % d
    if mod == 0:
        mod = d
    idx = (l - mod) // d
    step = (r - l) // d
    res = costs[mod][idx + step] - costs[mod][idx]
    print(res)

h, w, d = list(map(int, input().split()))
A = []
for _ in range(h):
    line = list(map(int, input().split()))
    A.append(line)
num_to_xy = [[0, 0] for i in range(h * w + 1)]
for y in range(h):
    for x in range(w):
        num = A[y][x]
        num_to_xy[num] = [x, y]
costs = [0] * (h * w + 1)
for start in range(1, d + 1):
    cost = 0
    num = start
    sx, sy = num_to_xy[num]
    while num <= h * w:
        nx, ny = num_to_xy[num]
        cost += abs(nx - sx) + abs(ny - sy)
        costs[num] = cost
        sx, sy = nx, ny
        num += d
q = int(eval(input()))
for _ in range(q):
    l, r = list(map(int, input().split()))
    print((costs[r] - costs[l]))

-costs = [[] for i in range(d + 1)] -for mod in range(1, d + 1): -    num = mod +costs = [0] * (h * w + 1) +for start in range(1, d + 1): +    cost = 0 +    num = start -    cost = 0 -        costs[mod].append(cost) +        costs[num] = cost +        sx, sy = nx, ny -        sx, sy = nx, ny -    mod = l % d -    if mod == 0: -        mod = d -    idx = (l - mod) // d -    step = (r - l) // d -    res = costs[mod][idx + step] - costs[mod][idx] -    print(res) +    print((costs[r] - costs[l]))