import sys

input = sys.stdin.readline

from collections import *



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

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

x, y = defaultdict(int), defaultdict(int)



for i in range(H):

    for j in range(W):

        x[A[i][j]] = i

        y[A[i][j]] = j



x[0] = x[D]

y[0] = y[D]

acc = [[0] for _ in range(D)]



for i in range(D):

    now = i

    

    while now+D<=H*W:

        acc[i].append(acc[i][-1]+abs(x[now+D]-x[now])+abs(y[now+D]-y[now]))

        now += D



Q = int(eval(input()))



for _ in range(Q):

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

    print((acc[L%D][R//D]-acc[L%D][L//D]))

    
import sys

input = sys.stdin.readline

from collections import *



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

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

x = [-1]*(H*W)

y = [-1]*(H*W)



for i in range(H):

    for j in range(W):

        x[A[i][j]-1] = i

        y[A[i][j]-1] = j



acc = [[0] for _ in range(D)]



for i in range(D):

    now = i

    

    while now+D<H*W:

        cost = abs(x[now]-x[now+D])+abs(y[now]-y[now+D])

        acc[i].append(acc[i][-1]+cost)

        now += D



Q = int(eval(input()))



for _ in range(Q):

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

    L -= 1

    R -= 1

    print((acc[L%D][R//D]-acc[L%D][L//D]))
import sys

input = sys.stdin.readline
from collections import *

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
x, y = defaultdict(int), defaultdict(int)
for i in range(H):
    for j in range(W):
        x[A[i][j]] = i
        y[A[i][j]] = j
x[0] = x[D]
y[0] = y[D]
acc = [[0] for _ in range(D)]
for i in range(D):
    now = i
    while now + D <= H * W:
        acc[i].append(acc[i][-1] + abs(x[now + D] - x[now]) + abs(y[now + D] - y[now]))
        now += D
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    print((acc[L % D][R // D] - acc[L % D][L // D]))

import sys

input = sys.stdin.readline
from collections import *

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
x = [-1] * (H * W)
y = [-1] * (H * W)
for i in range(H):
    for j in range(W):
        x[A[i][j] - 1] = i
        y[A[i][j] - 1] = j
acc = [[0] for _ in range(D)]
for i in range(D):
    now = i
    while now + D < H * W:
        cost = abs(x[now] - x[now + D]) + abs(y[now] - y[now + D])
        acc[i].append(acc[i][-1] + cost)
        now += D
Q = int(eval(input()))
for _ in range(Q):
    L, R = list(map(int, input().split()))
    L -= 1
    R -= 1
    print((acc[L % D][R // D] - acc[L % D][L // D]))

-x, y = defaultdict(int), defaultdict(int) +x = [-1] * (H * W) +y = [-1] * (H * W) -        x[A[i][j]] = i -        y[A[i][j]] = j -x[0] = x[D] -y[0] = y[D] +        x[A[i][j] - 1] = i +        y[A[i][j] - 1] = j -    while now + D <= H * W: -        acc[i].append(acc[i][-1] + abs(x[now + D] - x[now]) + abs(y[now + D] - y[now])) +    while now + D < H * W: +        cost = abs(x[now] - x[now + D]) + abs(y[now] - y[now + D]) +        acc[i].append(acc[i][-1] + cost) +    L -= 1 +    R -= 1