Buggy python submission (verdict: Runtime Error):
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 設立年で一度ソートする
py.sort(key = lambda x: x[2])
pcid = [1] * M # 各県の都市ID

for i in range(M):
    p = py[i][1] # 県ID
    py[i][2] = pcid[p-1]
    pcid[p-1] += 1

py.sort(key=lambda x:x[0])
for t in py:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))
    

Fixed submission:
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 設立年で一度ソートする
py.sort(key = lambda x: x[2])
pcid = [1] * N # 各県の都市ID

for i in range(M):
    p = py[i][1] # 県ID
    py[i][2] = pcid[p-1]
    pcid[p-1] += 1

py.sort(key=lambda x:x[0])
for t in py:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 全N個の県について調べる
Npy = []
for i in range(1,N+1):
    # 県i
    pyi = [j for j in py if j[1] == i] # 県iの市リスト
    pyi.sort(key=itemgetter(2)) # 県iの市を生まれた年でソート
    l = len(pyi)
    cnt = 0
    for k in range(1,l+1):
        pyi[k-1][2] = k
    Npy.extend(pyi)

Npy.sort(key=itemgetter(0))

for t in Npy:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))
    

Fixed submission:
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 設立年で一度ソートする
py.sort(key = lambda x: x[2])
pcid = [1] * N # 各県の都市ID

for i in range(M):
    p = py[i][1] # 県ID
    py[i][2] = pcid[p-1]
    pcid[p-1] += 1

py.sort(key=lambda x:x[0])
for t in py:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 全N個の県について調べる
Npy = []
for i in range(1,N+1):
    # 県i
    pyi = [j for j in py if j[1] == i] # 県iの市リスト
    pyi.sort(key=lambda x: x[2]) # 県iの市を生まれた年でソート
    l = len(pyi)
    cnt = 0
    for k in range(1,l+1):
        pyi[k-1][2] = k
    Npy.extend(pyi)

Npy.sort(key=lambda x: x[0])

for t in Npy:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))
    

Fixed submission:
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 設立年で一度ソートする
py.sort(key = lambda x: x[2])
pcid = [1] * N # 各県の都市ID

for i in range(M):
    p = py[i][1] # 県ID
    py[i][2] = pcid[p-1]
    pcid[p-1] += 1

py.sort(key=lambda x:x[0])
for t in py:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))

Buggy python submission (verdict: Runtime Error):
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 全N個の県について調べる
Npy = []
for i in range(1,N+1):
    # 県i
    pyi = [j for j in py if j[1] == i] # 県iの市リスト
    pyi.sort(key=lambda x: x[2]) # 県iの市を生まれた年でソート
    l = len(pyi)
    cnt = 0
    for k in range(1,l+1):
        pyi[k-1][2] = k
    Npy.extend(pyi)

Npy.sort(key=lambda x: x[0])

for t in Npy:
    print('{0:0>8}{1:0>8'.format(t[1],t[2]))
    

Fixed submission:
N, M = (int(i) for i in input().split(' '))
py=[]
p =[]

# 全都市を配列に代入
for i in range(M):
    p = [i]
    p.extend([int(j) for j in input().split(' ')])
    py.append(p)

# 設立年で一度ソートする
py.sort(key = lambda x: x[2])
pcid = [1] * N # 各県の都市ID

for i in range(M):
    p = py[i][1] # 県ID
    py[i][2] = pcid[p-1]
    pcid[p-1] += 1

py.sort(key=lambda x:x[0])
for t in py:
    print('{0:0>6}{1:0>6}'.format(t[1],t[2]))