Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    for i in range(x,y-1,d):
        ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
    print(ans)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    try:
        for i in range(x,y-1,d):
            ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
        print(ans)
    except:print(0)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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+1,j+1]
q=int(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    try:
        for i in range(x,y-1,d):
            ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
        print(ans)
    except:print(0)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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+1,j+1]
q=int(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    try:
        for i in range(x,y-1,d):
            ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
        print(ans)
    except:print(0)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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+1,j+1]
q=int(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    if x-y==0:print(0);continue
    try:
        for i in range(x,y-1,d):
            ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
        print(ans)
    except:print(0)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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+1,j+1]
q=int(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    ans=0
    if x-y==0:print(0);continue
    try:
        for i in range(x,y-1,d):
            ans +=abs(adress[i+d][0]-adress[i][0])+abs(adress[i+d][1]-adress[i][1])
        print(ans)
    except:print(0)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=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+1,j+1]
q=int(input())
order=[list(map(int,input().split())) for i in range(q)]
for x,y in order:
    point=[]
    ans=0
    if x-y==0:print(0);continue
    try:
        for i in range(x,y+1,d):
            point.append(adress[i])
        for j in range(len(point)-1):
            ans +=abs(point[j+1][0]-point[j][0])+abs(point[j+1][1]-point[j][1])
        print(ans)
    except:print(0)

Fixed submission:
H, W, D = map(int, input().split())
m = [None] * (H * W + 1)
for i in range(H):
    l = map(int, input().split())
    for j, a in enumerate(l):
        m[a] = (i, j)
Q = int(input())
LR = []
for _ in range(Q):
    l, r = map(int, input().split())
    LR.append((l, r))
 
S = [0] * (H * W + 1)
for k in range(D + 1, H * W + 1):
    x, y = m[k]
    i, j = m[k - D]
    S[k] = S[k - D] + abs(x - i) + abs(y - j)
 
for l, r in LR:
    print(S[r] - S[l])