Problem:
 Score: 300 points    Problem Statement In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.  City i is established in year Y_i and belongs to Prefecture P_i .  You can assume that there are no multiple cities that are established in the same year.  It is decided to allocate a 12 -digit ID number to each city.  If City i is the x -th established city among the cities that belong to Prefecture i , the first six digits of the ID number of City i is P_i , and the last six digits of the ID number is x .  Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.  Find the ID numbers for all the cities.  Note that there can be a prefecture with no cities.      Constraints  1 \leq N \leq 10^5  1 \leq M \leq 10^5  1 \leq P_i \leq N  1 \leq Y_i \leq 10^9  Y_i are all different.  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  M  P_1  Y_1  :  P_M  Y_M       Output Print the ID numbers for all the cities, in ascending order of indices (City 1 , City 2 , ... ).        Sample Input 1 2 3
1 32
2 63
1 12      Sample Output 1 000001000002
000002000001
000001000001   As City 1 is the second established city among the cities that belong to Prefecture 1 , its ID number is 000001000002 .  As City 2 is the first established city among the cities that belong to Prefecture 2 , its ID number is 000002000001 .  As City 3 is the first established city among the cities that belong to Prefecture 1 , its ID number is 000001000001 .        Sample Input 2 2 3
2 55
2 77
2 99      Sample Output 2 000002000001
000002000002
000002000003

Solution (Python):
N,M=map(int,input().split())

lists = [[] for _ in range(N)]
items = [list(map(int, input().split())) for _ in range(M)]
for p, y in items:
  lists[p-1].append(y)

dic = {}

for n in range(N):
  lists[n].sort()  

for n in range(N):
  l = lists[n]
  count = 1
  for i in l:
    dic[i] = count
    count+=1

for p, y in items:
  print("{:06g}{:06g}".format(p,dic[y]))

Solution (Python):
N, M = map(int, input().split())
l = []
for i in range(M):
  p, y = list(map(int, input().split()))
  l.append([i+1, p, y, ""])
l = sorted(l, key=lambda x: x[2])

cnt = [1]*(N+1)
for i in range(M):
  l[i][3] = str(l[i][1]).zfill(6) + str(cnt[l[i][1]]).zfill(6)
  cnt[l[i][1]] += 1

l = sorted(l, key=lambda x: x[0])
for i in range(M):
  print(l[i][3])

Solution (Python):
def main():
    N, M = map(int, input().split())
    citys = []
    for _ in range(M):
        p, y = map(int, input().split())
        citys.append((p, y))
    
    
    temp= [[] for _ in range(N)]
    for city in citys:
        p, y = city
        temp[p-1].append((p, y))
    sortedcitys = []
    for pre in temp:
        sortedcitys.append(sorted(pre, key=lambda x:x[1]))
    citysdict = {}
    for pre in sortedcitys:
        ban = 1
        for city in pre:
            citysdict[city] = ban
            ban += 1
    
    
    for city in citys:
        up = city[0]
        down = citysdict[city]
        print('{:0>6}'.format(up) + '{:0>6}'.format(down))

if __name__ == '__main__':
    main()

Solution (Python):
n,m = list(map(int,input().split()))
PY=[list(map(int,input().split()))for _ in range(m)]
id = [[]for _ in range(n)]
for a,b in PY:
    id[a-1].append(b)
for v in id:
    v.sort()
d = {}
for i in range(n):
    k=1
    for j in range(len(id[i])):
        d[id[i][j]] = k
        k+=1
for i,j in PY:
    print("{:06g}{:06g}".format(i,d[j]))

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()


def main():
    N, M = map(int, input().split())
    cities = []
    for i in range(M):
        P, Y = map(int, input().split())
        cities.append((i, P, Y))
    cities.sort(key=lambda t: t[2])
    count = defaultdict(int)
    ans = {}
    for i, P, _ in cities:
        count[P] += 1
        ans[i] = str(P).zfill(6) + str(count[P]).zfill(6)
    for i in range(M):
        print(ans[i])


main()

Solution (Python):
from collections import defaultdict
def main():
    N,M = map(int,input().split())
    P = defaultdict(list)
    for i in range(M):
        p,y = map(int,input().split())
        P[p].append([y,i])
    for t in P.keys():
        P[t].sort()
    ans = ['' for _ in range(M)]
    for v in P.items():
        pre = str(v[0])
        l = len(pre)
        while l < 6:
            pre = '0'+pre
            l += 1
        lis = v[1]
        for i,u in enumerate(lis):
            num = str(i+1)
            q = len(num)
            while q < 6:
                num = '0'+num
                q += 1
            ans[u[1]] = pre+num
    for a in ans:
        print(a)


if __name__ == "__main__":
    main()

Solution (Python):
N,M = map(int, input().split())
ID = [ list(map(int,input().split())) + [i] for i in range(M)]

ID.sort()
town = 0

for i in range(M):
    if ID[i][0] != town:
        town = ID[i][0]
        count = 1
        ID[i].append("0"*(6-len(str(town)))+str(town)+"0"*(6-len(str(count)))+str(count))
    else:
        count += 1
        ID[i].append("0"*(6-len(str(town)))+str(town)+"0"*(6-len(str(count)))+str(count))
        
ID.sort(key=lambda x:x[2])

for i in range(M):
    print(ID[i][3])

Solution (Python):
from operator import itemgetter
N, M = map(int, input().split())
P = [[0, 0, 0] for _ in range(M)]
for i in range(M):
    P[i][0], P[i][1] = map(int, input().split())
    P[i][0] = (6-len(str(P[i][0])))*"0"+str(P[i][0])
    
P_s = sorted(P, key=itemgetter(0, 1))

for i in range(1, M):
    if(P_s[i][0] == P_s[i-1][0]):
        P_s[i][2] = P_s[i-1][2]+1
    
for i in range(M):
    print(P[i][0]+(6-len(str(P[i][2]+1)))*"0"+str(P[i][2]+1))

Solution (Python):
n, m = map(int, input().split())
py = []
for i in range(m):
  py.append(list(map(int, input().split())) + [i])
  
py.sort(key=lambda x: x[1])
py.sort(key=lambda x: x[0])

bef = 0
state = 0

triple = [ ]
for i in py:
    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 ] )

Solution (Python):
n, m = map(int, input().split())
list_PY = [ list(map(int,input().split(" "))) for i in range(m)]

for i in range(m):
    l = list_PY[i]
    l.append(i)
list_PY.sort(key=lambda x:(x[0], x[1]))

cnt = 0
k = 0
for i in range(m):
    l = list_PY[i]
    if l[0] == k:
        cnt += 1
        a = len(list(str(l[0])))
        b = "0"*(6 - a) + str(l[0])
        c = len(list(str(cnt)))
        d = "0"*(6 - c) + str(cnt)
    else:
        k = l[0]
        cnt = 1
        a = len(list(str(l[0])))
        b = "0" * (6 - a) + str(l[0])
        c = len(list(str(cnt)))
        d = "0" * (6 - c) + str(cnt)
    l.append(b+d)


list_PY.sort(key=lambda x:x[2])

for i in range(m):
    print(list_PY[i][3])

Solution (Python):
N,M = map(int,(input().split()))
d = [[] for _ in range(N)]
P = [[] for _ in range(M)]
Y = [[] for _ in range(M)]

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

result = {}
for i in range(N):
    tmp = 1
    for j in d[i]:
        result[j] = tmp
        tmp += 1

def sixlize(n):
    k = len(str(n))
    return "0"*(6-k) + str(n)

for i in range(M):
    print( sixlize(P[i]) + sixlize(result[Y[i]]) )

Solution (Python):
N,M = map(int, input().split())
P = [list(map(int, input().split())) for _ in range(M)]
cnt = (N+1)*[0]
ids = {}

for p,y in sorted(P,key=lambda x: x[1]):
	cnt[p] += 1
	ids[y] = "%06d%06d" % (p,cnt[p])

for _,y in P:
	print(ids[y])

Solution (Python):
N, M = map(int,input().split())
p = []
pad = p.append
for i in range(M):
    s = list(map(int,input().split()))
    pad(s + [i])
p.sort()
cur = p[0][0]
cnt = 1
for i in range(M):
    if p[i][0] > cur:
        cur = p[i][0]
        cnt = 1
    p[i][1] = cnt
    cnt += 1
p.sort(key=lambda x: x[2])
for i in range(M):
    print("0" * (6 - len(str(p[i][0]))) + str(p[i][0]) + "0" * (6 - len(str(p[i][1]))) + str(p[i][1]))

Solution (Python):
N, M = map(int,input().split())
p = []
for i in range(M):
    s = list(map(int,input().split()))
    p.append(s + [i])
p.sort()
cur = p[0][0]
cnt = 1
for i in range(M):
    if p[i][0] > cur:
        cur = p[i][0]
        cnt = 1
    p[i][1] = cnt
    cnt += 1
p.sort(key=lambda x: x[2])
for i in range(M):
    print("0" * (6 - len(str(p[i][0]))) + str(p[i][0]) + "0" * (6 - len(str(p[i][1]))) + str(p[i][1]))

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import defaultdict

n, m = na()
py = nan(m)
d = defaultdict(list)

for i in range(m):
    p, y = py[i]
    d[p].append((y, i))

q = defaultdict(lambda: 1)
ans = []
for k, v in d.items():
    v.sort()
    for i in range(len(v)):
        y, j = v[i]
        ans.append((j, q[k], k))
        q[k] += 1

ans.sort()
for i in range(m):
    print("{:0>6}{:0>6}".format(ans[i][2], ans[i][1]))

Solution (Python):
n, m = map(int, input().split())
py_dict = dict()
ip_dict = dict()
io_dict = dict()

for i in range(m):
    py_list = list(map(int, input().split()))
    py_list.append(i)
    if py_list[0] in py_dict:
        py_dict[py_list[0]].append(py_list)
    else:
        py_dict[py_list[0]] = [py_list]
    ip_dict[i] = py_list[0]

for item in py_dict:
    tmp_list = py_dict[item]
    tmp_list.sort(key=lambda x: x[1])
    for i in range(len(tmp_list)):
        io_dict[tmp_list[i][2]] = str(
            ip_dict[tmp_list[i][2]]).zfill(6) + str(i+1).zfill(6)

for i in range(m):
    print(io_dict[i])

Solution (Python):
def K(n):
  a=0
  while n>0:
    n=n//10
    a=a+1
  return a

n,m=map(int,input().split())
A=[[0]*3 for i in range(m)]
ans=[0]*m
for i in range(m):
  A[i][0],A[i][1]=map(int,input().split())
  A[i][2]=i
A.sort()
a=0
b=1
for i in range(m):
  if a==A[i][0]:
    b=b+1
  else:
    a=A[i][0]
    b=1
  S=""
  T=""
  B=list(str(A[i][0]))
  C=list(str(b))
  for j in range(6-K(A[i][0])):
    S=S+"0"
  for j in range(K(A[i][0])):
    S=S+B[j]
  for j in range(6-K(b)):
    T=T+"0"
  for j in range(K(b)):
    T=T+C[j]
  ans[A[i][2]]=S+T
for i in range(m):
  print(ans[i])

Solution (Python):








import sys

sys.setrecursionlimit(10 ** 5 + 10)




def resolve():
    N,M=map(int,input().split())
    data=[list(map(int,input().split())) for i in range(M)]

    data1 = sorted(data, key=lambda x: x[1])
    cnt=[0]*N
    dic={}

    for [p,y] in data1:
        cnt[p-1]+=1
        dic[y]="%06d%06d"%(p,cnt[p-1])

    for [p,y] in data:
        print(dic[y])






resolve()

Solution (Python):








import sys
sys.setrecursionlimit(10 ** 5 + 10)


def resolve():
    N,M=map(int,input().split())
    P=[]
    city_count=[0]*N
    city_end=[1]*N
    for i in range(M):
        p,y=map(int,input().split())
        city_count[p-1]+=1
        P.append([p,y])

    P1 = sorted(P, key=lambda x: x[1])
    P1=sorted(P1)
    for i in P1:
        i.append(city_end[i[0]-1])
        city_end[i[0] - 1]+=1


    for i in P:
        print(str(i[0]).zfill(6)+str(i[2]).zfill(6))


    

resolve()

Solution (Python):


class City:
    
    def __init__(self,id,y):
        self.id = id
        self.year = y


def main():
    n,m = map(int,input().split())
    pre_lst = [[] for _ in range(n)]  
    city_tags = [0 for _ in range(m)] 

    for i in range(m):
        p,y = map(int,input().split())
        pre_lst[p-1].append(City(i,y)) 

    for pre_num,pre in enumerate(pre_lst):
        if len(pre) <= 0:
            continue
        pre.sort(key = lambda x:x.year) 

        counter = 0 
        for city in pre:
            city_tags[city.id] = str(pre_num+1).zfill(6) + str(counter+1).zfill(6)
            counter += 1
    for tag in city_tags:
        print(tag)


if __name__ == '__main__':
    main()

Solution (Python):
class City:
    def __init__(self,id,y):
        self.id = id
        self.year = y


def main():
    n,m = map(int,input().split())
    pre_lst = [[] for _ in range(n)]
    city_tags = [0 for _ in range(m)]

    for i in range(m):
        p,y = map(int,input().split())
        pre_lst[p-1].append(City(i,y))

    for pre_num,pre in enumerate(pre_lst):
        if len(pre) <= 0:
            continue
        pre.sort(key = lambda x:x.year)
        counter = 0
        for city in pre:
            city_tags[city.id] = str(pre_num+1).zfill(6) + str(counter+1).zfill(6)
            counter += 1
    for tag in city_tags:
        print(tag)


if __name__ == '__main__':
    main()

Solution (Python):
n, m = input().split()
n, m = int(n), int(m)

data = []

for i in range(m):
    temp_p, temp_y = input().split()
    temp_p, temp_y = int(temp_p), int(temp_y)
    data.append([temp_p, temp_y, i])

data.sort()

result = [[] for _ in range(m)]

i = data[0][0]

k = 1

for j in range(m):
    if i != data[j][0]:
        k = 1
        i = data[j][0]
    result[data[j][2]] = [data[j][0], k]
    k += 1

for temp_data in result:
    print("{:0>6}".format(temp_data[0]) + "{:0>6}".format(temp_data[1]))

Solution (Python):
import sys

N,M = (int(x) for x in input().split())
li = []
for i in range(M):
  a,b = (int(x) for x in input().split())
  li.append([i,a,b,""])

li.sort(key=lambda x: x[2])

cnt = [1]*N
for i in range(M):
  li[i][3] = str(li[i][1]).zfill(6) + str(cnt[li[i][1]-1]).zfill(6)
  cnt[li[i][1]-1] += 1
  
li.sort(key=lambda x: x[0])
for i in range(M):
  print(li[i][3])

Solution (Python):
import sys
input = sys.stdin.readline


def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1


n, m = map(int, input().split())
py = [list(map(int, input().split())) for i in range(m)]
check = sorted(py)
memo = py[0][0]
cnt = [0] * m
cnt_memo = 0

for i in range(m):
    if memo != check[i][0]:
        memo = check[i][0]
        cnt_memo = 0
    cnt_memo += 1
    cnt[i] = cnt_memo

for i in range(m):
    number = str(cnt[binary_search(check, py[i])])
    p = str(py[i][0])
    print((6 - len(p)) * '0' + p + (6 - len(number)) * '0' + number)

Solution (Python):
import collections
import bisect
n, m = map(int, input().split())
s = [[int(_) for _ in input().split() ] for _ in range(m)]
a = collections.defaultdict(list)
for x,y in sorted(s):
    a[x]+=[y]
for x,y in s:
    z = bisect.bisect(a[x],y)
    
    print("%06d%06d"%(x,z))

Solution (Python):
import collections
import bisect

n, m = map(int, input().split())
s = [[int(_) for _ in input().split() ] for _ in range(m)]
a = collections.defaultdict(list)

for x,y in sorted(s):
    a[x]+=[y]
for x,y in s:
    z = bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Solution (Python):
import collections
import bisect

n, m = map(int, input().split())
s = [[int(_) for _ in input().split() ] for _ in range(m)]
a = collections.defaultdict(list)

for x,y in sorted(s):
    a[x]+=[y]
for x,y in s:
    z = bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Solution (Python):
import collections
import bisect

n, m = map(int, input().split())

s = [[int(_) for _ in input().split() ] for _ in range(m)]
a = collections.defaultdict(list)

for x,y in sorted(s):
    a[x]+=[y]
for x,y in s:
    z = bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Solution (Python):
def solve():
    n, m = list(map(int, input().split()))

    p = [[] for _ in range(m + 1)]
    y = [[] for _ in range(m + 1)]

    pref = [[] for _ in range(n + 1)]
    
    for i in range(m):
        _p, _y = list(map(int, input().split()))
        p[i] = _p
        y[i] = _y

        pref[_p].append(_y)

    for i in range(len(pref)):
        pref[i] = list((sorted(pref[i])))

    _pref = [[] for _ in range(len(pref))]
    for i in range(len(_pref)):
        _pref[i] = dict(zip(pref[i], range(1, len(pref[i]) + 1)))


    for i in range(m):
        print("{:06}{:06}".format(p[i], _pref[p[i]][y[i]]))

if __name__ == "__main__":
    solve()

Solution (Python):
from sys import stdin
def f(n,m):
    n,m=str(n),str(m)
    s1=["0"]*(6-len(n))
    s1.append(n)
    s2=["0"]*(6-len(m))
    s2.append(m)
    return "".join(s1)+"".join(s2)

def main():
    
    readline=stdin.readline
    N,M=map(int,readline().split())
    li=[0]*M
    for i in range(M):
        a=list(map(int,readline().split()))
        a.append(i)
        li[i]=a
    
    li.sort(key=lambda x:x[1])
    count=[1]*(N+1)
    for i in range(M):
        city_number=li[i][0]
        x=count[city_number]
        count[city_number]+=1
        li[i].append(f(city_number,x))

    li.sort(key=lambda x:x[2])
    for i in range(M):
        print(li[i][3])
        
if __name__=="__main__":
    main()

Solution (Python):
from collections import defaultdict
N,M=map(int,input().split())
dic=defaultdict(list)
DIC=defaultdict(str)
l=[]
for i in range(M):
  i,x=map(int,input().split())
  l.append([i,x])
  dic[i].append(x)
for i in range(1,N+1):
  half_l='0'*(6-len(str(i)))+str(i)
  if len(dic[i])==0:
    pass
  else:
    sorted_l=sorted(dic[i])
    index_dic={}
    index=1
    for num in sorted_l:
      index_dic[num]=index
      index+=1
    for j in range(len(dic[i])):
      INDEX=index_dic[dic[i][j]]
      half_r='0'*(6-len(str(INDEX)))+str(INDEX)
      DIC[(i,dic[i][j])]=half_l+half_r
for i in range(M):
  i,x=l[i][0],l[i][1]
  print(DIC[(i,x)])

Solution (Python):

n, m = map(int, input().split())
data = [[] for _ in range(n)]
for i in range(m):
    p, y = map(int, input().split())
    data[p - 1].append([y, i])

ans = [None] * m
for i in  range(n):
    data[i].sort()
    for j in range(len(data[i])):
        y, k = data[i][j]
        ans[k] = '0' * (6 - len(str(i + 1))) + str(i + 1) + '0' * (6 - len(str(j + 1))) + str(j + 1)

for i in range(len(ans)):
    print(ans[i])

Solution (Python):
N, M = [int(x) for x in input().split()]

CITYID = 2
PREF = 0
YEAR = 1
city = [[int(x) for x in input().split()] + [i] for i in range(M)]

city.sort()
id = [""] * M
ord = 1 

for i in range(M):
    if i > 0 and city[i][PREF] == city[i - 1][PREF]:
        ord += 1
    else:
        ord = 1
    id[city[i][CITYID]] = '{:0>6}{:0>6}'.format(str(city[i][PREF]),  str(ord))

for i in range(M):
    print(id[i])

Solution (Python):
n, m = map(int, input().split())
City = sorted([tuple(map(int, input().split())) + (i, ) for i in range(m)])

Ans = [None] * m
pre_p = City[0][0]
cnt = 1
for p, y, i in City:
    if pre_p != p:
        cnt = 1
        pre_p = p
    Ans[i] = '0'*(6 - len(str(p))) + str(p) + '0'*(6 - len(str(cnt))) + str(cnt)
    cnt += 1
print(*Ans, sep='\n')

Solution (Python):
n, m = map(int, input().split())
ary = []
for i in range(m):
    
    ary.append([int(x) for x in input().split()])
    ary[i].append(i) 


ary.sort(key=lambda x:(x[0], x[1]))

p = 1
num = 1
for v in ary:
    if v[0] != p:
        p = v[0]
        num = 1
    _id = str(v[0]).zfill(6) + str(num).zfill(6)
    num += 1
    v.append(_id)

ary.sort(key=lambda x:x[2]) 

for v in ary:
    print(v[3])

Solution (Python):


import collections
from bisect import bisect


def main():
    N, M = map(int, input().split())
    PY = [[int(j) for j in input().split()] for i in range(M)]

    a = collections.defaultdict(list)
    for x, y in sorted(PY):
        a[x] += [y]

    for x, y in PY:
        z = bisect(a[x], y)
        print("%06d%06d" % (x, z))


if __name__ == "__main__":
    main()

Solution (Python):
n, m = map(int, input().split())

a = []
for i in range(m):
  
  
  
  a.append(list(map(int, input().split())))
  a[i].append(i)


a.sort(key=lambda x:(x[0], x[1]))
p = 0
num = 1
for v in a:
  if not v[0] == p:
    p = v[0]
    num = 1
  _id = str(v[0]).zfill(6) + str(num).zfill(6)
  num += 1
  v.append(_id)

a.sort(key=lambda x:x[2])

for v in a:
  print(v[3])

Solution (Python):
from collections import defaultdict
import bisect

N,M = map(int, input().split())
PY = [tuple(map(int, input().split())) for _ in range(M)]

prefs = defaultdict(list)


for p, y in PY:
    
    prefs[p].append(y)

for k in prefs.keys():
    
    prefs[k].sort()

for p, y in PY:
    idx = bisect.bisect_left(prefs[p], y)
    print(str(p).zfill(6) + str(idx+1).zfill(6))

Solution (Python):
N,M = map(int, input().split())
PY = [[0] * 2 for i in range(M)]

for i in range(M):
    p,y = map(int,input().split())
    PY[i][0] = p
    PY[i][1] = y

indices = list(range(M))
sorted_indices = sorted(indices, key=lambda i: PY[i])
PY.sort()

def calc(x):
    n = len(str(x))
    res = ""
    if n < 6:
        for i in range(6-n):
            res += "0"
    res += str(x)
    return res

before = 0
num = 1
ans = [0]*M
for i in range(M):
    if before != PY[i][0]:
        num = 1
        ans[sorted_indices[i]] = calc(PY[i][0])+calc(num)
        num += 1
        before = PY[i][0]
    else:
        ans[sorted_indices[i]] = calc(PY[i][0])+calc(num)
        num += 1
        before = PY[i][0]

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

Solution (Python):
n,m = map( int, input().split())
  
result= {}
input_list = [[] for i in range(n)]


for i in range(m):
  p,y = map(int, input().split())
  input_list[p-1].append((y,i))
  
for i in input_list :
  i.sort()

result = [None]*m

for p,i in enumerate(input_list) :
  for n, (a,b) in enumerate(i):
    result[b] = str(p+1).zfill(6)+str(n+1).zfill(6)
    
print(*result, sep = '\n')

Solution (Python):
from operator import itemgetter
n,m=map(int,input().split())
l=[]
for i in range(m):
  g=list(map(int,input().split()))
  l.append(g)
  l[i].append(i)
l.sort()
l[0][1]=1
for i in range(1,m):
  if l[i][0]==l[i-1][0]:
    l[i][1]=int(l[i-1][1])+1
  else:
    l[i][1]=1
newl=sorted(l,key=itemgetter(2))
for i in range(m):  
  print((6-len(str(newl[i][0])))*'0'+str(newl[i][0])+(6-len(str(newl[i][1])))*'0'+str(newl[i][1]))

Solution (Python):
n, m = [int(i) for i in input().split()]
data = []  
for i in range(m):
    arr = [int(k) for k in input().split()]
    data.append(arr + [i])

data.sort()
id = []  
now_p = rank = 0
for p, _, o in data:
    if now_p != p:
        now_p = p
        rank = 1
    id.append([p, rank, o])
    rank += 1

id.sort(key=lambda x: x[2])
for p, x, _ in id:
    print('{:06}{:06}'.format(p, x))

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

YPI.sort()
ans = [''] * M
num_city = {}
for y, p, i in YPI:
  if p in num_city:
    num_city[p] += 1
  else:
    num_city[p] = 1
  
  ans[i] = str(p).zfill(6) + str(num_city[p]).zfill(6)

print(*ans, sep='\n')

Solution (Python):
from collections import defaultdict

def main():
    n, m = [int(e) for e in input().split()]
    PY = [[int(e) for e in input().split()] for i in range(m)]

    Ds = defaultdict(set)
    for p, y in PY:
        Ds[p].add(y)

    Dd = defaultdict(dict)
    for p, s in Ds.items():
        for i, y in enumerate(sorted(s)):
            Dd[p][y] = i + 1

    for p, y in PY:
        print('{:06}{:06}'.format(p, Dd[p][y]))

if __name__ == "__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
pp=[0]*6
xx=[0]*6
PY=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n+1)]
for i ,(p,y) in enumerate(PY):
    g[p].append((y,i))

ans=[0]*m
for i in range(n+1):
    for o,(j,k) in enumerate(sorted(g[i])):
        ans[k]=str(i).zfill(6)+str(o+1).zfill(6)
for i in ans:
    print(i)

Solution (Python):


def read_input():
    n, m = map(int, input().split())
    pylist = [tuple(map(int, input().split())) for _ in range(m)]
    return n, m, pylist


def submit():
    n, m, pylist = read_input()
    pylist_count = {}

    pylist_sort = sorted(pylist, key=lambda x: x[1])
    cities = {i: 0 for i in range(1, n + 1)}

    for p, y in pylist_sort:
        cities[p] += 1
        pylist_count[(p, y)] = cities[p]
    
    for p, y in pylist:
        print(str(p).zfill(6) + str(pylist_count[(p, y)]).zfill(6))


if __name__ == "__main__":
    submit()

Solution (Python):
n,m=map(int,input().split())
num=[[] for i in range(n)]
p=[0]*m
y=[0]*m
for i in range(m):
  p[i],y[i]=map(int,input().split())
  num[p[i]-1].append((i,y[i]))
score=['']*m
for i in range(n):
  num[i].sort(key=lambda x:x[1])
  idx=1
  for s,t in num[i]:
    score[s]=str(i+1).zfill(6)+str(idx).zfill(6)
    idx+=1
print(*score,sep='\n')

Solution (Python):
n, m = map(int, input().split())
L = [tuple(map(int, input().split())) for _ in range(m)]
cnt = [0]*(n+1)
IDs = {}
for p, y in sorted(L, key = lambda x: x[1]):
    cnt[p] += 1
    
    
    IDs[y] = '%06d%06d' % (p, cnt[p])
for _, y in L:
    print(IDs[y])

Solution (Python):
from collections import defaultdict
import bisect
N, M = map(int, input().split())
PY = []
PYd = defaultdict(list)
for i in range(M):
    x, y = map(int, input().split())
    PYd[x].append(y)
    PY.append((x,y))

for i in PYd.keys():
    PYd[i].sort()

for p,y in PY:
    str_p = str(p).zfill(6)
    index = str(bisect.bisect(PYd[p],y)).zfill(6)
    print(str_p + index)

Solution (Python):
from collections import defaultdict
import bisect
N, M = map(int, input().split())
PY = []
PYd = defaultdict(list)
for i in range(M):
    x, y = map(int, input().split())
    PYd[x].append(y)
    PY.append((x,y))

for i in PYd.keys():
    PYd[i].sort()

for p,y in PY:
    str_p = str(p).zfill(6)
    index = str(bisect.bisect(PYd[p],y)).zfill(6)
    print(str_p + index)

Solution (Python):
n,m=map(int,input().split())

mlist=[]
for i in range(m):
    mlist.append(list(map(int,input().split())))
    mlist[i].append(i)

mlist.sort(key=lambda x:x[1])

tlist={}

for indlist in mlist:
    ken=indlist[0]
    if ken in tlist:
        indlist[1]=tlist[ken]+1
        tlist[ken] = tlist[ken]+1
    else:
        indlist[1]=1
        tlist[ken]=1

mlist.sort(key=lambda x:x[2])

for result in mlist:
    pref=str(result[0])
    ord=str(result[1])
    output=""
    leng=len(pref)
    for i in range(6-leng):
        output +="0"
    output += pref

    leng=len(ord)
    for i in range(6-leng):
        output +="0"
    output += ord

    print(output)

Solution (Python):
import collections
import bisect

n, m = map(int, input().split())

s = [[int(_) for _ in input().split() ] for _ in range(m)]
a = collections.defaultdict(list)

for x,y in sorted(s):
    a[x]+=[y]
for x,y in s:
    z = bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Solution (Python):
import sys
input = sys.stdin.readline
N, M = map(int,input().split())
P = []
for i in range(M) :
    p, y = map(int,input().split())
    P.append([p, y, i])

P.sort(key = lambda x: (x[0],x[1]))

ans = [0 for i in range(M)]
count = 1

for i in range(M) :
    num = P[i][2]
    if i > 0 :
        if P[i][0] == P[i-1][0] :
            count += 1
        else :
            count = 1
    ans[num] = str(P[i][0]).zfill(6) + str(count).zfill(6)

for t in ans :
    print(t)

Solution (Python):
n, m = map(int, input().split())
py = [list(map(int, input().split())) + [i] for i in range(m)]
py.sort()

ans = []
for i in range(m):
    p = py[i][0]
    y = py[i][1]
    key = py[i][2]
    if i == 0:
        ans.append([p, 1, key])
    elif i >= 1:
        if ans[i - 1][0] == p:
            ans.append([p, ans[i - 1][1] + 1, key])
        else:
            ans.append([p, 1, key])

ans = sorted(ans, key=lambda x: x[2])
for i in range(m):
    print(format(ans[i][0], '06'), end='')
    print(format(ans[i][1], '06'))

Solution (Python):
N, M = map(int, input().split())
P = [[] for _ in range(N)]
PY = []

for _ in range(M):
    p, y = map(int, input().split())
    PY.append((p - 1, y))
    P[p - 1].append(y)


PD = [{} for _ in range(N)]

for i in range(N):
    for j, c in enumerate(sorted(P[i]), 1):
        PD[i][c] = j
    

for p, y in PY:
    print('%06d%06d' % (p + 1, PD[p][y]))

Solution (Python):


N,M = list(map(int, input().rstrip().split()))
PY_list = [list(map(int, input().rstrip().split())) for i in range(M)]




pref_dic={}

for p,y in PY_list:
    pref_dic.setdefault(p,[])
    pref_dic[p] += [y]

for k in pref_dic:
    pref_dic[k].sort()



city_dic={}

for k in pref_dic:
    for i,year in enumerate( pref_dic[k] ):
        city_dic.setdefault(year,-1)
        city_dic[year] = i

for m in range(M):
    inum_pref = PY_list[m][0]
    
    inum_city = city_dic[ PY_list[m][1] ] + 1
    print('{0:06}{1:06}'.format(inum_pref, inum_city))

Solution (Python):
from bisect import bisect_left as bl


def f(v):
    v = str(v)
    return '0' * (6 - len(v)) + v


N, M = map(int, input().split())
dic = dict()
PY = []
for i in range(M):
    p, y = map(int, input().split())
    if dic.get(p, False):
        dic[p].append(y)
    else:
        dic[p] = [y]
    PY.append((p, y))

for i in range(1, N + 1):
    if dic.get(i, False):
        dic[i] = sorted(dic[i])


for i in range(M):
    p, y = PY[i]
    
    l = dic[p]
    ans = f(p) + f(bl(l, y) + 1)
    print(ans)

Solution (Python):
def main():
    N, M = map(int, input().split())
    P = [[] for _ in range(N)]
    PY = []
    for _ in range(M):
        p, y = map(int, input().split())
        PY.append((p - 1, y))
        P[p - 1].append(y)
    PD = [{} for _ in range(N)]
    for i in range(N):
        for j, c in enumerate(sorted(P[i]), 1):
            PD[i][c] = j
    for p, y in PY:
        print('%06d%06d' % (p + 1, PD[p][y]))

main()

Solution (Python):
n,m = map(int,input().split())
py = [list(map(int,input().split()))+[_] for _ in range(m)]
py.sort()
flag = py[0][0]
flag_2 = 1
for i in range(m):
  if flag != py[i][0]:
    flag = py[i][0]
    flag_2 = 1
  py[i][0] = (6-len(str(py[i][0])))*"0"+str(py[i][0])
  py[i][1] = (6-len(str(flag_2)))*"0"+str(flag_2)
  flag_2 += 1
py.sort(key = lambda py:py[2])
for j in range(m):
  print("".join(py[j][:2]))

Solution (Python):
n,m = map(int, input().split())
lst = [[] for _ in range(n)]

for i in range(m):
    p,y = map(int, input().split())
    lst[p-1].append((y,i))

ans = [None]*m

for i,d in enumerate(lst):
    d.sort()
    for j, (Y,I) in enumerate(d):
        ans[I] = str(i+1).zfill(6) + str(j+1).zfill(6)

for i in ans:
    print(i)

Solution (Python):
from operator import itemgetter
n,m = map(int,input().split())
lists = [list(map(int,input().split())) for i in range(m)]
for i in range(m):
    lists[i].append(i)
sort_lists = sorted(lists,key=itemgetter(0,1))
cnt = 1
ans = []
for i in range(m-1):
    tmp = sort_lists[i][0]
    left =  '{0:06d}'.format(tmp)
    right = '{0:06d}'.format(cnt)
    if tmp == sort_lists[i+1][0]:
        cnt += 1
    else:
        cnt = 1
    ans.append([str(left) + str(right),sort_lists[i][2]] )
tmp = sort_lists[-1][0]
left = '{0:06d}'.format(tmp)
right = '{0:06d}'.format(cnt)
ans.append([str(left) + str(right), sort_lists[-1][2] ])
ans.sort(key = itemgetter(1))
for i in range(m):
    print(ans[i][0])