Buggy python submission (verdict: Runtime Error):
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
for i in range(d):
    numm[i]=0
for i in range(d,h*w):
    numm[i]=numm[i-d]+abs(num[i-1][0]-num[i-d-1][0])+abs(num[i-1][1]-num[i-d-1][1])

q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    print(numm[r]-numm[l])

Fixed submission:
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
for i in range(d):
    numm[i]=0
for i in range(d,h*w):
    numm[i]=numm[i-d]+abs(num[i][0]-num[i-d][0])+abs(num[i][1]-num[i-d][1])

q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    print(numm[r-1]-numm[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
#for i in range(h*w-d):
    #numm[i]=abs(num[i+d-1][0]-num[i-1][0])+abs(num[i+d-1][1]-num[i-1][1])
q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    result=0
    while l!=r:
        if numm[l]==None:
            numm[l]=abs(num[l+d-1][0]-num[l-1][0])+abs(num[l+d-1][1]-num[l-1][1])
        result+=numm[l]
        l+=d
    print(result)

Fixed submission:
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
for i in range(d):
    numm[i]=0
for i in range(d,h*w):
    numm[i]=numm[i-d]+abs(num[i][0]-num[i-d][0])+abs(num[i][1]-num[i-d][1])

q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    print(numm[r-1]-numm[l-1])

Buggy python submission (verdict: Runtime Error):
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
for i in range(h*w-d):
    numm[i]=abs(num[i+d-1][0]-num[i-1][0])+abs(num[i+d-1][1]-num[i-1][1])
q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    result=0
    while l!=r:
        result+=numm[l]
        l+=d
    print(result)

Fixed submission:
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
for i in range(d):
    numm[i]=0
for i in range(d,h*w):
    numm[i]=numm[i-d]+abs(num[i][0]-num[i-d][0])+abs(num[i][1]-num[i-d][1])

q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    print(numm[r-1]-numm[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    result=0
    while l!=r:
        result+=abs(num[l+d-1][0]-num[l-1][0])+abs(num[l+d-1][1]-num[l-1][1])
        l+=d
    print(result)

Fixed submission:
h,w,d=(int(i) for i in input().split())
num=[None for i in range(h*w)]
for i in range(h):
    a=list(map(int,input().split()))
    for j in range(w):
        num[a[j]-1]=(i,j)
numm=[None for i in range(h*w)]
for i in range(d):
    numm[i]=0
for i in range(d,h*w):
    numm[i]=numm[i-d]+abs(num[i][0]-num[i-d][0])+abs(num[i][1]-num[i-d][1])

q=int(input())
for i in range(q):
    l,r=list(map(int,input().split()))
    print(numm[r-1]-numm[l-1])