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



dic = {}

K = H*W//D

memo = [[0]* (K+1) for _ in range(D+1)]



for y in range(1, H+1):

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

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

        dic[a] = [x, y]



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

    dic[extra] = [0, 0]



for i in range(1, D+1):

    for j in range(K):

        x, y = dic[i+D*j]

        nx, ny = dic[i+D*(j+1)]

        cost = abs(x-nx) + abs(y-ny)

        memo[i][j+1] = memo[i][j] + cost



Q = int(eval(input()))

for _ in range(Q):

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

    i = L % D

    j = L//D

    delta = (R-L)//D

    if i == 0:

        i = D

        j -= 1

    ans = memo[i][j+delta] - memo[i][j]

    print(ans)

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()))
dic = {}
K = H * W // D
memo = [[0] * (K + 1) for _ in range(D + 1)]
for y in range(1, H + 1):
    A = list(map(int, input().split()))
    for x, a in enumerate(A, 1):
        dic[a] = [x, y]
for extra in range(H * W + 1, H * W + 2 * D):
    dic[extra] = [0, 0]
for i in range(1, D + 1):
    for j in range(K):
        x, y = dic[i + D * j]
        nx, ny = dic[i + D * (j + 1)]
        cost = abs(x - nx) + abs(y - ny)
        memo[i][j + 1] = memo[i][j] + cost
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    i = L % D
    j = L // D
    delta = (R - L) // D
    if i == 0:
        i = D
        j -= 1
    ans = memo[i][j + delta] - memo[i][j]
    print(ans)

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())) -dic = {} -K = H * W // D -memo = [[0] * (K + 1) for _ in range(D + 1)] -for y in range(1, H + 1): -    A = list(map(int, input().split())) -    for x, a in enumerate(A, 1): -        dic[a] = [x, y] -for extra in range(H * W + 1, H * W + 2 * D): -    dic[extra] = [0, 0] -for i in range(1, D + 1): -    for j in range(K): -        x, y = dic[i + D * j] -        nx, ny = dic[i + D * (j + 1)] -        cost = abs(x - nx) + abs(y - ny) -        memo[i][j + 1] = memo[i][j] + cost -Q = int(eval(input())) -for _ in range(Q): -    L, R = list(map(int, input().split())) -    i = L % D -    j = L // D -    delta = (R - L) // D -    if i == 0: -        i = D -        j -= 1 -    ans = memo[i][j + delta] - memo[i][j] -    print(ans) +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)