Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import math

N, M = map(int, input().split())
P = [None] * M
Y = [None] * M
for i in range(M):
    p, y = map(int, input().split())
    P[i] = p
    Y[i] = y

ken = [None] * N
for i in range(N):
    ken[i] = []

for i in range(M):
    ken[P[i] - 1].append(Y[i])


# < M * log(N)
for i in range(N):
    ken[i] = sorted(ken[i])


def close_twopower(n):
    output = 2
    while output < n:
        output = output * 2
    return output


def comp(tr, node, x, i):
    if tr[i] is None:
        return i
    elif x < tr[i][0]:
        return comp(tr, node, x, 2 * i + 1)
    else:
        return comp(tr, node, x, 2 * i + 2)


# 木構造
def create_bitree(a):
    # a = [[int, hoge, ...], ...]
    n = len(a)
    output = [None] * int(math.pow(2, n))

    for i in range(n):
        # output = comp(output, a[i], a[i][0], 0)
        output[comp(output, a[i], a[i][0], 0)] = a[i]

    return output


city_info = [None] * M
k = 0

# count M
while(k < M):
    for l in range(N):
        for m in range(len(ken[l])):
            # ken[l][m]の値によって二分木を作る
            # create_bitree()

            city_info[k] = [ken[l][m], l + 1, m + 1]
            k += 1

bi_city_info = create_bitree(city_info)


def create_id(p, x):
    output_p = str(p)
    output_x = str(x)
    p_len = len(output_p)
    x_len = len(output_x)
    while p_len < 6:
        output_p = "0" + output_p
        p_len += 1
    while x_len < 6:
        output_x = "0" + output_x
        x_len += 1

    return output_p + output_x


def comp2(tr, x, i):
    if tr[i][0] == x:
        return i
    elif x < tr[i][0]:
        return comp2(tr, x, 2 * i + 1)
    else:
        return comp2(tr, x, 2 * i + 2)

    return 0  # dummy


def bi_search(tr, x):
    # tr is [[int. hoge, ...]
    return comp2(tr, x, 0)


# 律速
# M^2
for i in range(M):
    # Y[i]を探してきてprint
    j = bi_search(bi_city_info, Y[i])
    print(create_id(bi_city_info[j][1], bi_city_info[j][2]))


Fixed submission:
# -*- coding: utf-8 -*-

# from time import time
# 
# start = time()


def six(n):
    n_str = str(n)
    while len(n_str) < 6:
        n_str = "0" + n_str

    return n_str


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

id_list = [None] * M
dic_P = {}
info = []
info_idx = 0

for i in range(M):
    P, Y = map(int, input().split())
    id_list[i] = [P, Y, ""]
    if P in dic_P.keys():
        info[dic_P[P]].append([i, P, Y])
    else:
        dic_P[P] = info_idx
        info_idx += 1
        info.append([[i, P, Y]])

for group in info:
    group.sort(key=lambda a_i: a_i[2], reverse=False)
    for i, pair in enumerate(group):
        id = six(pair[1]) + six(i+1)
        id_list[pair[0]] = id

for id in id_list:
    print(id)

# print(time() - start)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
P = [None] * M
Y = [None] * M
for i in range(M):
    p, y = map(int, input().split())
    P[i] = p
    Y[i] = y

ken = [None] * N
for i in range(N):
    ken[i] = []

for i in range(M):
    ken[P[i] - 1].append(Y[i])

for i in range(N):
    ken[i] = sorted(ken[i])

city_info = [None] * M

k = 0


# 木構造
# def create_bitree(a):
#     # a = int list
#     n = len(a)
#     output = [None] * n
#
#     for i in range(n):
#         if a[i] < _:
#             output[sub_bitree()] = a[i]
#         else:
#             output[sub_bitree()] = a[i]


# count M
while(k < M):
    for l in range(N):
        for m in range(len(ken[l])):
            # ken[l][m]の値によって二分木を作る
            # if city_info[0] is None:
            #     city_info[0] = [ken[l][m], l + 1, m + 1]
            # else:

            city_info[k] = [ken[l][m], l + 1, m + 1]
            k += 1


def create_id(p, x):
    output_p = str(p)
    output_x = str(x)
    p_len = len(output_p)
    x_len = len(output_x)
    while p_len < 6:
        output_p = "0" + output_p
        p_len += 1
    while x_len < 6:
        output_x = "0" + output_x
        x_len += 1

    return output_p + output_x


# 律速？
for i in range(M):
    # Y[i]を探してきてprint
    for j in range(len(city_info)):
        if city_info[j][0] == Y[i]:
            print(create_id(city_info[j][1], city_info[j][2]))
            del city_info[j]
            break


Fixed submission:
# -*- coding: utf-8 -*-

# from time import time
# 
# start = time()


def six(n):
    n_str = str(n)
    while len(n_str) < 6:
        n_str = "0" + n_str

    return n_str


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

id_list = [None] * M
dic_P = {}
info = []
info_idx = 0

for i in range(M):
    P, Y = map(int, input().split())
    id_list[i] = [P, Y, ""]
    if P in dic_P.keys():
        info[dic_P[P]].append([i, P, Y])
    else:
        dic_P[P] = info_idx
        info_idx += 1
        info.append([[i, P, Y]])

for group in info:
    group.sort(key=lambda a_i: a_i[2], reverse=False)
    for i, pair in enumerate(group):
        id = six(pair[1]) + six(i+1)
        id_list[pair[0]] = id

for id in id_list:
    print(id)

# print(time() - start)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
P = [None] * M
Y = [None] * M
for i in range(M):
    p, y = map(int, input().split())
    P[i] = p
    Y[i] = y

ken = [None] * N
for i in range(N):
    ken[i] = []

for i in range(M):
    ken[P[i] - 1].append(Y[i])

for i in range(N):
    ken[i] = sorted(ken[i])

city_info = [None] * M

k = 0

while(k < M):
    for l in range(N):
        for m in range(len(ken[l])):
            city_info[k] = [ken[l][m], l + 1, m + 1]
            k += 1


def createID(p, x):
    output_p = str(p)
    output_x = str(x)
    p_len = len(output_p)
    x_len = len(output_x)
    while p_len < 6:
        output_p = "0" + output_p
        p_len += 1
    while x_len < 6:
        output_x = "0" + output_x
        x_len += 1

    return output_p + output_x


for i in range(M):
    # Y[i]を探してきてprint
    for j in range(M):
        if city_info[j][0] == Y[i]:
            print(createID(city_info[j][1], city_info[j][2]))
            break


Fixed submission:
# -*- coding: utf-8 -*-

# from time import time
# 
# start = time()


def six(n):
    n_str = str(n)
    while len(n_str) < 6:
        n_str = "0" + n_str

    return n_str


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

id_list = [None] * M
dic_P = {}
info = []
info_idx = 0

for i in range(M):
    P, Y = map(int, input().split())
    id_list[i] = [P, Y, ""]
    if P in dic_P.keys():
        info[dic_P[P]].append([i, P, Y])
    else:
        dic_P[P] = info_idx
        info_idx += 1
        info.append([[i, P, Y]])

for group in info:
    group.sort(key=lambda a_i: a_i[2], reverse=False)
    for i, pair in enumerate(group):
        id = six(pair[1]) + six(i+1)
        id_list[pair[0]] = id

for id in id_list:
    print(id)

# print(time() - start)