Buggy python submission (verdict: Time Limit Exceeded):

H, W, D = map(int, input().split())

coord_dict_x = [0 for i in range(90002)]
coord_dict_y = [0 for i in range(90002)]

for i in range(H):
    gyo = [int(i) for i in input().split()]
    for j in range(W):
        coord_dict_x[gyo[j]] = i
        coord_dict_y[gyo[j]] = j

Q = int(input())
exam = [[int(i) for i in input().split()] for i in range(Q)]

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1_x = coord_dict_x[num]
    coord1_y = coord_dict_y[num]
    while num < exam[i][1]:
        num += D
        coord2_x = coord_dict_x[num]
        coord2_y = coord_dict_y[num]
        reduce_MP += abs(coord1_x - coord2_x) + abs(coord1_y - coord2_y)
        coord1_x, coord1_y = coord2_x, coord2_y
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Runtime Error):

H, W, D = map(int, input().split())

coord_dict_x = [0 for i in range(90001)]
coord_dict_y = [0 for i in range(90001)]

for i in range(H):
    gyo = [int(i) for i in input().split()]
    for j in range(W):
        coord_dict_x[gyo[j]] = i
        coord_dict_y[gyo[j]] = j

Q = int(input())
exam = [[int(i) for i in input().split()] for i in range(Q)]

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1_x = coord_dict_x[num]
    coord1_y = coord_dict_y[num]
    while num < exam[i][1]:
        num += D
        coord2_x = coord_dict_x[num]
        coord2_y = coord_dict_y[num]
        reduce_MP += abs(coord1_x - coord2_x) + abs(coord1_y - coord2_y)
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Time Limit Exceeded):

H, W, D = map(int, input().split())

coord_dict = {}
for i in range(H):
    gyo = [int(i) for i in input().split()]
    coord_dict.update({gyo[j] : (i,j) for j in range(W)})

Q = int(input())
exam = [[int(i) for i in input().split()] for i in range(Q)]

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1 = coord_dict[num]
    while num < exam[i][1]:
        num += D
        coord2 = coord_dict[num]
        reduce_MP += abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1])
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Time Limit Exceeded):

H, W, D = map(int, input().split())

coord_dict = {}
for i in range(H):
    gyo = [int(i) for i in input().split()]
    for j in range(W):
        coord_dict.update({gyo[j] : (i,j)})
        
Q = int(input())

"""
exam = [[] for i in range(Q)]
for i in range(Q):
    exam[i] = [int(i) for i in input().split()]
"""
exam = [[int(i) for i in input().split()] for i in range(Q)]

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1 = coord_dict[num]
    while num < exam[i][1]:
        num += D
        coord2 = coord_dict[num]
        reduce_MP += abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1])
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Time Limit Exceeded):

H, W, D = map(int, input().split())

coord_dict = {}
for i in range(H):
    gyo = [int(i) for i in input().split()]
    for j in range(W):
        coord_dict.update({gyo[j] : (i,j)})
        
Q = int(input())

exam = [[] for i in range(Q)]
for i in range(Q):
    exam[i] = [int(i) for i in input().split()]

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1 = coord_dict[num]
    while num < exam[i][1]:
        coord2 = coord_dict[num + D]
        reduce_MP += abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1])
        num += D
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Time Limit Exceeded):

H, W, D = map(int, input().split())
masu = [[] for i in range(H)]
for i in range(H):
    masu[i] = tuple([int(i) for i in input().split()])
masu = tuple(masu)
Q = int(input())

exam = [[] for i in range(Q)]
for i in range(Q):
    exam[i] = [int(i) for i in input().split()]

coord_dict = {}
for i in range(H):
    for j in range(W):
        coord_dict.update({masu[i][j] : (i,j)})

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1 = coord_dict[num]
    while num < exam[i][1]:
        coord2 = coord_dict[num + D]
        reduce_MP += abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1])
        num += D
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Time Limit Exceeded):
from  functools import lru_cache

@lru_cache(maxsize = 2**17)
def get_coord(num, H, W, masu):
    for i in range(H):
        for j in range(W):
            if masu[i][j] == num:
                return i, j

H, W, D = map(int, input().split())
masu = [[] for i in range(H)]
for i in range(H):
    masu[i] = tuple([int(i) for i in input().split()])
masu = tuple(masu)
Q = int(input())

exam = [[] for i in range(Q)]
for i in range(Q):
    exam[i] = [int(i) for i in input().split()]

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1 = get_coord(num, H, W, masu)
    while num < exam[i][1]:
        coord2 = get_coord(num + D, H, W, masu)
        reduce_MP += abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1])
        num += D
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])

Buggy python submission (verdict: Time Limit Exceeded):

def get_coord(num, H, W, masu):
    for i in range(H):
        for j in range(W):
            if masu[i][j] == num:
                return i, j

H, W, D = map(int, input().split())
masu = []
for i in range(H):
    masu.append([int(i) for i in input().split()])
Q = int(input())

exam = []
for i in range(Q):
    exam.append([int(i) for i in input().split()])

for i in range(Q):
    reduce_MP = 0
    num = exam[i][0]
    coord1 = get_coord(num, H, W, masu)
    while num < exam[i][1]:
        coord2 = get_coord(num + D, H, W, masu)
        reduce_MP += abs(coord1[0] - coord2[0]) + abs(coord1[1] - coord2[1])
        num += D
        coord1 = coord2
    print(reduce_MP)



Fixed submission:
H, W, D = list(map(int, input().split()))

Amap = []
for i in range(H):
    Amap.append(list(map(int, input().split())))

xx = [0 for i in range(90002)]
yy = [0 for i in range(90002)]
for y in range(H):
    for x in range(W):
        xx[Amap[y][x]] = x
        yy[Amap[y][x]] = y
Q = int(input())

coord = []
for i in range(Q):
    coord.append(list(map(int, input().split())))

# magic point calculation
mp = [0 for i in range(90002)]

for i in range(D+1, 90002):
    mp[i] = mp[i-D] + abs(xx[i] -xx[i-D]) + abs(yy[i] - yy[i-D])

for i in coord:
    print(mp[i[1]] - mp[i[0]])