def dist(x,y):

    return abs(x[0]-y[0])+abs(x[1]-y[1])

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

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

G = {}

for i in range(H):

    for j in range(W):

        G[A[i][j]] = (i,j)

C = {}

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

    k = 1

    C[j] = 0

    while j+k*D<=H*W:

        C[j+k*D] = C[j+(k-1)*D]+dist(G[j+(k-1)*D],G[j+k*D])

        k += 1

Q = int(eval(input()))

for _ in range(Q):

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

    print((C[R]-C[L]))
H,W,D = list(map(int,input().split()))

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

C = {i:0 for i in range(1,H*W+1)}

for i in range(H):

    for j in range(W):

        C[A[i][j]] = (i,j)

dist = {i:[] for i in range(1,D+1)}

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

    dist[i].append(0)

    y0,x0 = C[i]

    for j in range(1,(H*W-i)//D+1):

        y1,x1 = C[i+j*D]

        dist[i].append(dist[i][-1]+abs(y1-y0)+abs(x1-x0))

        y0 = y1

        x0 = x1

Q = int(eval(input()))

for _ in range(Q):

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

    i = L%D

    if i==0:

        i = D

    j1 = (L-i)//D

    j2 = (R-i)//D

    print((dist[i][j2]-dist[i][j1]))
def dist(x, y):
    return abs(x[0] - y[0]) + abs(x[1] - y[1])


H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
G = {}
for i in range(H):
    for j in range(W):
        G[A[i][j]] = (i, j)
C = {}
for j in range(1, D + 1):
    k = 1
    C[j] = 0
    while j + k * D <= H * W:
        C[j + k * D] = C[j + (k - 1) * D] + dist(G[j + (k - 1) * D], G[j + k * D])
        k += 1
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    print((C[R] - C[L]))

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
C = {i: 0 for i in range(1, H * W + 1)}
for i in range(H):
    for j in range(W):
        C[A[i][j]] = (i, j)
dist = {i: [] for i in range(1, D + 1)}
for i in range(1, D + 1):
    dist[i].append(0)
    y0, x0 = C[i]
    for j in range(1, (H * W - i) // D + 1):
        y1, x1 = C[i + j * D]
        dist[i].append(dist[i][-1] + abs(y1 - y0) + abs(x1 - x0))
        y0 = y1
        x0 = x1
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    i = L % D
    if i == 0:
        i = D
    j1 = (L - i) // D
    j2 = (R - i) // D
    print((dist[i][j2] - dist[i][j1]))

-def dist(x, y): -    return abs(x[0] - y[0]) + abs(x[1] - y[1]) - - -G = {} +C = {i: 0 for i in range(1, H * W + 1)} -        G[A[i][j]] = (i, j) -C = {} -for j in range(1, D + 1): -    k = 1 -    C[j] = 0 -    while j + k * D <= H * W: -        C[j + k * D] = C[j + (k - 1) * D] + dist(G[j + (k - 1) * D], G[j + k * D]) -        k += 1 +        C[A[i][j]] = (i, j) +dist = {i: [] for i in range(1, D + 1)} +for i in range(1, D + 1): +    dist[i].append(0) +    y0, x0 = C[i] +    for j in range(1, (H * W - i) // D + 1): +        y1, x1 = C[i + j * D] +        dist[i].append(dist[i][-1] + abs(y1 - y0) + abs(x1 - x0)) +        y0 = y1 +        x0 = x1 -    print((C[R] - C[L])) +    i = L % D +    if i == 0: +        i = D +    j1 = (L - i) // D +    j2 = (R - i) // D +    print((dist[i][j2] - dist[i][j1]))