Buggy python submission (verdict: Time Limit Exceeded):
H,W,D=map(int,input().split())
A=[[0,0] for i in range(H*W+1)]
for i in range(H):
    a=list(map(int,input().split()))
    for j in range(W):
        A[a[j]]=[i+1,j+1]
#print(A)

Q=int(input())
for x in range(Q):
    L,R=map(int,input().split())
    P=A[L]
    if L==R:
        print(0)
    else:
        ans=0
        for y in range(H*W):
            if L+(y+1)*D==R:
                ans+=(abs(A[L+y*D][0]-A[L+(y+1)*D][0])+abs(A[L+y*D][1]-A[L+(y+1)*D][1]))
                print(ans)
                break
            else:
                ans+=(abs(A[L+y*D][0]-A[L+(y+1)*D][0])+abs(A[L+y*D][1]-A[L+(y+1)*D][1]))

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
H,W,D=map(int,input().split())
Li=[]

for i in range(1,H+1):
    s=list(map(int,input().split()))
    for j in range(1,W+1):
        Li.append([s[j-1],i,j])
Li.sort()
#print(Li)
Q=int(input())
for x in range(Q):
     L,R=map(int,input().split())
     sum=0
     for y in range(1000000):
         if L==R:
             print(sum)
             break
         else:
             sum+=abs(Li[L-1][1]-Li[L-1+D][1])+abs(Li[L-1][2]-Li[L-1+D][2])
             L+=D

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
H,W,D=map(int,input().split())
list1=[]
for i in range(H*W+1):
    list1.append([0,0])

#print(list1)

for j in range(H):
    A=list(map(int,input().split()))
    for k in range(W):
        list1[A[k]][0]=j+1
        list1[A[k]][1]=k+1
#print(list1)

Q=int(input())
for a in range(Q):
    L,R=map(int,input().split())
    cnt=0
    while L<=R:
        if L==R:
            print(cnt)
            break
        else:
            
            cnt+=abs(list1[L+D][0]-list1[L][0])+abs(list1[L+D][1]-list1[L][1])
            L+=D
            

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])
 
list1=[[0,0]]*(H*W)
for i in range(H):
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        list1[int(line[j])-1]=[i,j]
#print(list1)
 
 
    
N=int(input())
#print(N)
 
for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            L1=list1[L-1]
            L2=list1[L+D-1]
            #print(L1)
            #print(L2)
            count=count+abs(list1[L-1][0]-list1[L+D-1][0])+abs(list1[L-1][1]-list1[L+D-1][1])
            L=L+D
        print(int(count))

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])
 
list1=[[0,0]]*(H*W)
for i in range(H):
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        list1[int(line[j])-1]=[i,j]
#print(list1)
 
 
    
N=int(input())
#print(N)
 
for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            L1=list1[L-1]
            L2=list1[L+D-1]
            #print(L1)
            #print(L2)
            count=count+abs(list1[L-1][0]-list1[L+D-1][0])+abs(list1[L-1][1]-list1[L+D-1][1])
            L=L+D
        print(int(count))

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])

list1=[[0,0]]*(H*W)
for i in range(H):
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        list1[int(line[j])-1]=[i,j]
#print(list1)


    
N=int(input())
#print(N)

for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            L1=list1[L-1]
            L2=list1[L+D-1]
            #print(L1)
            #print(L2)
            count=count+((L1[0]-L2[0])**2)**0.5+((L1[1]-L2[1])**2)**0.5
            L=L+D
        print(int(count))
    

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])

list1=[[0,0]]*(H*W)
for i in range(H):
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        list1[int(line[j])-1]=[i,j]
#print(list1)


    
N=int(input())
#print(N)

for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            L1=list1[L-1]
            L2=list1[L+D-1]
            #print(L1)
            #print(L2)
            count=count+((L1[0]-L2[0])**2)**0.5+((L1[1]-L2[1])**2)**0.5
            L=L+D
        print(int(count))
    

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])

list1=[[0,0]]*(H*W)
for i in range(H):
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        list1[int(line[j])-1]=[i,j]
#print(list1)


    
N=int(input())
#print(N)

for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            L1=list1[L-1]
            L2=list1[L+D-1]
            #print(L1)
            #print(L2)
            count=count+((L1[0]-L2[0])**2)**0.5+((L1[1]-L2[1])**2)**0.5
            L=L+D
        print(int(count))
    

Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])

Buggy python submission (verdict: Time Limit Exceeded):
HWD=input()
HWD=HWD.rstrip().split(" ")
H=int(HWD[0])
W=int(HWD[1])
D=int(HWD[2])

list1=[]
for i in range(H):
    list2=[]
    line=input()
    line=line.rstrip().split(" ")
    for j in range(W):
        
        list2.append(int(line[j]))
    list1.append(list2)
#print(list1)


    
N=int(input())
#print(N)

for k in range(N):
    LR=input()
    LR=LR.rstrip().split(" ")
    L=int(LR[0])
    R=int(LR[1])
    if L==R:
        print(0)
    else:
        count=0
        for l in range(int((R-L)/D)):
            
            a=0
            for a in range(H):
                if L in list1[a]:
                    L1=[a+1,list1[a].index(L)+1]
            b=0    
            for b in range(H):
                if L+D in list1[b]:
                    L2=[b+1,list1[b].index(L+D)+1]
            
            count=count+((L1[0]-L2[0])**2)**0.5+((L1[1]-L2[1])**2)**0.5
            L=L+D
        print(int(count))


Fixed submission:
H,W,D=map(int,input().split())
L=[0 for i in range(H*W+1)]
A=[]
for i in range(H):
    a=list(map(int,input().split()))
    A.append(a)
    
for i in range(H):
    for j in range(W):
        L[A[i][j]]=[i+1,j+1]
L[0]=[0,0]
#print(L)
l=[0 for i in range(H*W+1)]
for i in range(D):
    for j in range(H*W+1):
        if (i+j*D)>(H*W):
            break
        elif j==0:
            l[i+j*D]=0
        else:
            l[i+j*D]=l[i+(j-1)*D]+abs(L[i+j*D][0]-L[i+(j-1)*D][0])+abs(L[i+j*D][1]-L[i+(j-1)*D][1])
#print(l)
Q=int(input())
for i in range(Q):
    a,b=map(int,input().split())
    print(l[b]-l[a])