Problem p03221 (python)

Fixed submission:
N, M = map ( int, input ( ).split ( ) )# N,Mを取得
#初期化
lists = [ ]
#listに要素を追加　後で並び変えるため並び替える前の順番を示すキーを第三要素に挿入しておく
for i in range ( M ):
    lists.append ( list ( map ( int, input ( ).split ( ) ) ) + [ i ] )
#第一要素に昇順で並び替えた後、第二要素で昇順に並び替える
lists.sort ( key=lambda x: x[ 1 ] )
lists.sort ( key=lambda x: x[ 0 ] )
# befは前回の第一要素　stateは第一要素での順位
bef = 0
state = 0
retstr = ''
triple = [ ]#第四要素に識別番号を追加したリストの初期化
for i in lists:
    if (i[ 0 ] != bef):
        bef = i[ 0 ]
        state = 1
        triple.append ( [ i[ 0 ], i[ 1 ], i[ 2 ], str ( i[ 0 ] ).rjust ( 6, '0' ) + str ( state ).rjust ( 6, '0' ) ] )
    else:
        state += 1
        triple.append ( [ i[ 0 ], i[ 1 ], i[ 2 ], str ( i[ 0 ] ).rjust ( 6, '0' ) + str ( state ).rjust ( 6, '0' ) ] )
 
triple.sort ( key=lambda x: x[ 2 ] )#第三要素で並び替えて、元の入力に対応した式ベンツ番号の順番に戻す
for i in triple:
    print ( i[ 3 ] )#識別番号の出力

Buggy submission (verdict: Runtime Error):
debug = 0
"""
N: state num
M: city num
"""


if debug:
    N = 2
    m = 10
else:
    (N, M) = list(map(int, input().split()))

if debug:
    p_y_list = [
        [1, 30]
        ,[1, 20]
        ,[1, 10]
        ,[1, 3]
        ,[2, 63]
        ,[2, 33]
        ,[2, 23]
        ,[3, 21]
        ,[3, 7]
    ]

city_id_list = []
# for z in range(M):
for (P, Y) in p_y_list:
    
    if not debug:
        (P, Y) = list(map(int, input().split()))

    city_id_list.append([Y, P])
    
city_id_list = sorted( city_id_list, key= lambda x: x[0])

state_cnt = {}
tmp_list = []
for (Y, P) in city_id_list:
    
    if P not in state_cnt.keys():
        state_cnt[P] = 1
    elif P in state_cnt.keys():
        state_cnt[P] += 1
        
    tmp_id = str(P).zfill(6) + str(state_cnt[P]).zfill(6)
    
    tmp_list.append([state_cnt[P], tmp_id])
    
for cid in sorted(tmp_list, key=lambda x: x[0]):
    print(cid[1])

Buggy submission (verdict: Runtime Error):
"""
N: state num
M: city num
"""

state_cnt = {}
city_id_dict = {}

(N, M) = list(map(int, input().split()))
# N = 3
# m = 3

for z in range(M):
# p_y_list = [
#     [1, np.random.randint(100)]
#     ,[3, np.random.randint(100)]
#     ,[2, np.random.randint(100)]
#     ,[2, np.random.randint(100)]
#     ,[1, np.random.randint(100)]
#     ,[2, np.random.randint(100)]
    
# ]
# for (P, Y) in p_y_list:
    (P, Y) = list(map(int, input().spilt()))

    if P not in state_cnt.keys():
        state_cnt[P] = 1
    elif P in state_cnt.keys():
        state_cnt[P] += 1
        
    state_num = str(P).zfill(6)
    city_num = str(state_cnt[P]).zfill(6)
    tmp_id = state_num + city_num
    city_id_dict[Y] = tmp_id
    
# get_state = set(map(int, state_cnt.keys()))
# all_state = set(range(1, N+1, 1))
# remain = set(all_state) - set(get_state)
# for num in ramain:
#     tmp_id = str(num).zfill(6) + "0"*6
#     city_id_list.append(tmp_id)
    
for _, city_id in sorted(city_id_dict.items()):
    print(city_id)

Buggy submission (verdict: Runtime Error):
"""
N: state num
M: city num
"""

state_cnt = {}
city_id_dict = {}

(N, M) = list(map(int, input().split()))

for z in range(M):
    (P, Y) = list(map(int, input().spilt()))

    if P not in state_cnt.keys():
        state_cnt[P] = 1
    elif P in state_cnt.keys():
        state_cnt[P] += 1
        
    state_num = str(P).zfill(6)
    city_num = str(Y).zfill(6)
    tmp_id = state_num + city_num
    city_id_dict[Y] = tmp_id
    
# get_state = set(map(int, state_cnt.keys()))
# all_state = set(range(1, N+1, 1))
# remain = set(all_state) - set(get_state)
# for num in ramain:
#     tmp_id = str(num).zfill(6) + "0"*6
#     city_id_list.append(tmp_id)
    
[print(city_id) for _, city_id in sorted(city_id_dict.items())]

Buggy submission (verdict: Runtime Error):
state_cnt = {}
city_id_dict = {}

(N, M) = list(map(int, input().split()))

for z in range(M):
    (P, Y) = list(map(int, input().spilt()))
    city_num_list.append(Y)

    if P not in state_cnt.keys():
        state_cnt[P] = 1
    elif P in state_cnt.keys():
        state_cnt[P] += 1
        
    state_num = str(P).zfill(6)
    city_num = str(state_cnt[P]).zfill(6)
    tmp_id = state_num + city_num
    city_id_dict[Y] = tmp_id
    
# get_state = set(map(int, state_cnt.keys()))
# all_state = set(range(1, N+1, 1))
# remain = set(all_state) - set(get_state)
# for num in ramain:
#     tmp_id = str(num).zfill(6) + "0"*6
#     city_id_list.append(tmp_id)
    
[print(city_id) for _, city_id in sorted(city_id_dict.items())]