Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

def main():
    N, M = map(int, readline().rstrip().split())
    inp = [list(map(int, readline().rstrip().split())) for _ in range(M)]
    pref = {}
    for p, y in inp:
        pref.setdefault(p, [])
        pref[p].append(y)

    for key, val in pref.items():
        pref[key] = sorted(val)
        
    res = []
    for p, city in inp:
        cities = pref[p]
        id = str(p).zfill(6) + str(cities.index(city)+1).zfill(6)
        res.append(id)
    
    print(*res, sep='\n')

if __name__ == '__main__':
    main()

Fixed submission:
from operator import itemgetter

def main():
    num_prefecture, num_city = list(map(int, input().rstrip().split()))
    prefecture_list = []
    for i in range(num_prefecture):
        prefecture_list.append([])
    #print(prefecture_list)
    id_list = []
    for idx_city in range(num_city):
        p_belong, y_born = list(map(int, input().rstrip().split()))
        p_belong -= 1 # 0から始まるようにする
        prefecture_list[p_belong].append([idx_city, y_born])
    
    #print(prefecture_list)
    # 各県のリスト内で, 市の誕生順にソートする
    for l in prefecture_list:
        l.sort(key = itemgetter(1))
    
    #print(prefecture_list)
    
    for idx_p, l in enumerate(prefecture_list):
        for x, [idx_city, y_born] in enumerate(l):
            id_first = '{0:06d}'.format(idx_p + 1)
            id_end = '{0:06d}'.format(x + 1)
            id_num = str(id_first) + str(id_end)
            id_list.append([idx_city, id_num])
    
    id_list.sort(key = itemgetter(0))
    
    for id_num in id_list:
        print(id_num[1])
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

def main():
    N, M = map(int, readline().rstrip().split())
    inp = [list(map(int, readline().rstrip().split())) for _ in range(M)]
    pref = {}
    for p, y in inp:
        pref.setdefault(p, [])
        pref[p].append(y)

    res = []
    for p, city in inp:
        cities = sorted(pref[p])
        id = str(p).zfill(6) + str(cities.index(city)+1).zfill(6)
        res.append(id)
    
    print(*res, sep='\n')

if __name__ == '__main__':
    main()

Fixed submission:
from operator import itemgetter

def main():
    num_prefecture, num_city = list(map(int, input().rstrip().split()))
    prefecture_list = []
    for i in range(num_prefecture):
        prefecture_list.append([])
    #print(prefecture_list)
    id_list = []
    for idx_city in range(num_city):
        p_belong, y_born = list(map(int, input().rstrip().split()))
        p_belong -= 1 # 0から始まるようにする
        prefecture_list[p_belong].append([idx_city, y_born])
    
    #print(prefecture_list)
    # 各県のリスト内で, 市の誕生順にソートする
    for l in prefecture_list:
        l.sort(key = itemgetter(1))
    
    #print(prefecture_list)
    
    for idx_p, l in enumerate(prefecture_list):
        for x, [idx_city, y_born] in enumerate(l):
            id_first = '{0:06d}'.format(idx_p + 1)
            id_end = '{0:06d}'.format(x + 1)
            id_num = str(id_first) + str(id_end)
            id_list.append([idx_city, id_num])
    
    id_list.sort(key = itemgetter(0))
    
    for id_num in id_list:
        print(id_num[1])
    
if __name__ == '__main__':
    main()