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

box=[list(map(int,input().split())) for i in range(h)]

adress={}

for i in range(h):

    for j in range(w):

        adress[box[i][j]]=[i,j]

q=int(eval(input()))

order=[list(map(int,input().split())) for i in range(q)]

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

for k in range(d+1,h*w+1):

    x,y=adress[k]

    i,j=adress[k-d]

    S[k]=S[k-d]+abs(x-i)+abs(y-j)

for x,y in order:

    print((S[y]-S[x]))
h,w,d=list(map(int,input().split()))

box=list(list(map(int,input().split())) for i in range(h))

q=int(eval(input()))

order=[list(map(int,input().split())) for i in range(q)]



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

add={}



for i in range(h):

    for j in range(w):

        add[box[i][j]]=[i,j]



for k in range(d+1,h*w+1):

    x,y=add[k]

    i,j=add[k-d]

    S[k]=S[k-d]+abs(x-i)+abs(y-j)



for s,g in order:

    print((S[g]-S[s]))
h, w, d = list(map(int, input().split()))
box = [list(map(int, input().split())) for i in range(h)]
adress = {}
for i in range(h):
    for j in range(w):
        adress[box[i][j]] = [i, j]
q = int(eval(input()))
order = [list(map(int, input().split())) for i in range(q)]
S = [0] * (h * w + 1)
for k in range(d + 1, h * w + 1):
    x, y = adress[k]
    i, j = adress[k - d]
    S[k] = S[k - d] + abs(x - i) + abs(y - j)
for x, y in order:
    print((S[y] - S[x]))

h, w, d = list(map(int, input().split()))
box = list(list(map(int, input().split())) for i in range(h))
q = int(eval(input()))
order = [list(map(int, input().split())) for i in range(q)]
S = [0] * (h * w + 1)
add = {}
for i in range(h):
    for j in range(w):
        add[box[i][j]] = [i, j]
for k in range(d + 1, h * w + 1):
    x, y = add[k]
    i, j = add[k - d]
    S[k] = S[k - d] + abs(x - i) + abs(y - j)
for s, g in order:
    print((S[g] - S[s]))

-box = [list(map(int, input().split())) for i in range(h)] -adress = {} -for i in range(h): -    for j in range(w): -        adress[box[i][j]] = [i, j] +box = list(list(map(int, input().split())) for i in range(h)) +add = {} +for i in range(h): +    for j in range(w): +        add[box[i][j]] = [i, j] -    x, y = adress[k] -    i, j = adress[k - d] +    x, y = add[k] +    i, j = add[k - d] -for x, y in order: -    print((S[y] - S[x])) +for s, g in order: +    print((S[g] - S[s]))