Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline

def main():
    H,W,D = map(int,input().split())
    a = [list(map(int,input().split())) for _ in range(H)]
    pos = [[-1,-1] for _ in range(H*W)]
    for i in range(H):
        for j in range(W):
            pos[a[i][j]-1][0] = i
            pos[a[i][j]-1][1] = j
    
    def manhattan_dist(x1,y1,x2,y2):
        return abs(x2-x1)+abs(y2-y1)

    ans_list = [[0] for _ in range(D)]
    for i in range(D):
        do = -(-(H*W-i)//D)
        for j in range(do-1):
            x1,y1 = pos[D*j+i]
            x2,y2 = pos[D*(j+1)+i]
            md = manhattan_dist(x1,y1,x2,y2)
            ans_list[i].append(ans_list[i][-1]+md)
    
    Q = int(input())
    for _ in range(Q):
        a,b = map(int,input().split())
        mod = (a-1)%D
        print(ans_list[mod][(b-mod)//D]-ans_list[mod][(a-mod)//D])

if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

def main():
    H,W,D = map(int,input().split())
    A = [list(map(int,input().split())) for _ in range(H)]
    Q = int(input())
    test = [list(map(int,input().split())) for _ in range(Q)]
    
    at = [None for _ in range(H*W)]
    for x in range(H):
        for y in range(W):
            at[A[x][y]-1] = (x,y)
            
    def move(x,y):
        dx = abs(at[y][0]-at[x][0])
        dy = abs(at[y][1]-at[x][1])
        return dx + dy
            
    cost = [[0 for _ in range(-(-H*W//D))] for _ in range(D)]
    for i in range(D):
        do = -(-(H*W-i)//D)
        for j in range(do-1):
            cost[i][j+1] = cost[i][j] + move(D*j+i,D*(j+1)+i)
            
    for i in range(Q):
        start,goal = test[i]
        v = start % D - 1
        u = -(-start // D) - 1
        w = -(-goal // D) - 1
        print(cost[v][w]-cost[v][u])
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline

def main():
    H,W,D = map(int,input().split())
    a = [list(map(int,input().split())) for _ in range(H)]
    pos = [[-1,-1] for _ in range(H*W)]
    for i in range(H):
        for j in range(W):
            pos[a[i][j]-1][0] = i
            pos[a[i][j]-1][1] = j
    
    def manhattan_dist(x1,y1,x2,y2):
        return abs(x2-x1)+abs(y2-y1)

    ans_list = [[0] for _ in range(D)]
    for i in range(D):
        for j in range((H*W-i-1)//D):
            x1,y1 = pos[D*j+i]
            x2,y2 = pos[D*(j+1)+i]
            md = manhattan_dist(x1,y1,x2,y2)
            ans_list[i].append(ans_list[i][-1]+md)
    
    Q = int(input())
    for _ in range(Q):
        a,b = map(int,input().split())
        mod = (a-1)%D
        print(ans_list[mod][(b-mod)//D]-ans_list[mod][(a-mod)//D])

if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline

def main():
    H,W,D = map(int,input().split())
    A = [list(map(int,input().split())) for _ in range(H)]
    Q = int(input())
    test = [list(map(int,input().split())) for _ in range(Q)]
    
    at = [None for _ in range(H*W)]
    for x in range(H):
        for y in range(W):
            at[A[x][y]-1] = (x,y)
            
    def move(x,y):
        dx = abs(at[y][0]-at[x][0])
        dy = abs(at[y][1]-at[x][1])
        return dx + dy
            
    cost = [[0 for _ in range(-(-H*W//D))] for _ in range(D)]
    for i in range(D):
        do = -(-(H*W-i)//D)
        for j in range(do-1):
            cost[i][j+1] = cost[i][j] + move(D*j+i,D*(j+1)+i)
            
    for i in range(Q):
        start,goal = test[i]
        v = start % D - 1
        u = -(-start // D) - 1
        w = -(-goal // D) - 1
        print(cost[v][w]-cost[v][u])
    
if __name__ == "__main__":
    main()