Buggy python submission (verdict: Time Limit Exceeded):
n, m = [int(i) for i in input().split()]
pre = []
year = []
year_order = [0 for i in range(m)]
each_arr = [[] for i in range(n)]
each_sort = [[] for i in range(n)]

for i in range(m):
    p, y = [int(i) for i in input().split()]
    pre.append(p)
    year.append(y)


for i in range(m):
    each_arr[pre[i]-1].append(year[i])

for j in range(n):
    each_sort[j] = sorted(each_arr[j])

for i in range(m):
    year_order[i] = each_sort[pre[i]-1].index(each_arr[pre[i]-1][0])+1
    each_arr[pre[i]-1].remove(each_arr[pre[i]-1][0])

for i in range(m):
    print("{0:06d}".format(pre[i])+"{0:06d}".format(year_order[i]))

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
L = []
for i in range(M):
    P, Y = map(int, input().split())
    L.append([P, Y, i])

L = sorted(L)

reset = 0
count = 0
ans = []
for i in range(M):
    if L[i][0] != reset:
        reset = L[i][0]
        count = 1
    else:
        count += 1
    
    ans.append([L[i][2], str(reset).zfill(6) + str(count).zfill(6)])

ans = sorted(ans)

for i in range(M):
    print(ans[i][1])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = [int(i) for i in input().split()]
pre = []
year = []
year_order = [0 for i in range(m)]
each_arr = [[] for i in range(n)]
each_sort = [[] for i in range(n)]

for i in range(m):
    p, y = [int(i) for i in input().split()]
    pre.append(p)
    year.append(y)


for i in range(m):
    each_arr[pre[i]-1].append(year[i])

for j in range(n):
    each_sort[j] = sorted(each_arr[j])

for i in range(m):
    year_order[i] = each_sort[pre[i]-1].index(each_arr[pre[i]-1][0])+1
    each_arr[pre[i]-1].remove(each_arr[pre[i]-1][0])

pre = [format(str(i), '0>6') for i in pre]
year_order = [format(str(i), '0>6') for i in year_order]

for i in range(m):
    print(pre[i]+year_order[i])

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
L = []
for i in range(M):
    P, Y = map(int, input().split())
    L.append([P, Y, i])

L = sorted(L)

reset = 0
count = 0
ans = []
for i in range(M):
    if L[i][0] != reset:
        reset = L[i][0]
        count = 1
    else:
        count += 1
    
    ans.append([L[i][2], str(reset).zfill(6) + str(count).zfill(6)])

ans = sorted(ans)

for i in range(M):
    print(ans[i][1])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = [int(i) for i in input().split()]
pre = []
year = []
year_order = [0 for i in range(m)]
each_arr = [[] for i in range(n)]
each_sort = [[] for i in range(n)]
each_order = [[] for i in range(n)]

for i in range(m):
    p, y = [int(i) for i in input().split()]
    pre.append(p)
    year.append(y)


for i in range(m):
    each_arr[pre[i]-1].append(year[i])

for j in range(n):
    each_sort[j] = sorted(each_arr[j])
    each_order[j] = [each_sort[j].index(i)+1 for i in each_arr[j]]

for i in range(m):
    year_order[i] = each_order[pre[i]-1][0]
    each_order[pre[i]-1].remove(each_order[pre[i]-1][0])

pre = [format(str(i), '0>6') for i in pre]
year_order = [format(str(i), '0>6') for i in year_order]

for i in range(m):
    print(pre[i]+year_order[i])

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
L = []
for i in range(M):
    P, Y = map(int, input().split())
    L.append([P, Y, i])

L = sorted(L)

reset = 0
count = 0
ans = []
for i in range(M):
    if L[i][0] != reset:
        reset = L[i][0]
        count = 1
    else:
        count += 1
    
    ans.append([L[i][2], str(reset).zfill(6) + str(count).zfill(6)])

ans = sorted(ans)

for i in range(M):
    print(ans[i][1])