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):

import sys


def solve(N: int, M: int, P: "List[int]", Y: "List[int]"):
    from itertools import count
    from collections import defaultdict
    Q = sorted(zip(Y, P, count(0)))
    c = defaultdict(int)
    ans = [None] * M
    for y, p, i in Q:
        c[p] += 1
        ans[i] = str(p).zfill(6) + str(c[p]).zfill(6)
    return ans



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    P = [int()] * (M)  
    Y = [int()] * (M)  
    for i in range(M):
        P[i] = int(next(tokens))
        Y[i] = int(next(tokens))
    print(*solve(N, M, P, Y), sep='\n')

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

Solution (Python):
def c_id():
    from collections import defaultdict
    N, M = [int(i) for i in input().split()]
    Cities = [[int(i) for i in input().split()] for j in range(M)]

    dic = defaultdict(list)
    
    for p, y in Cities:
        dic[p].append(y)
    
    for p, lst in dic.items():
        dic[p] = {v: k for k, v in enumerate(sorted(lst), 1)}
    return '\n'.join(['{0:06d}{1:06d}'.format(p, dic[p][y]) for p, y in Cities])

print(c_id())

Solution (Python):
n,m=map(int,input().split())
temp=[0]*m
tempn=[]
for i in range(0,m):
  p,y=map(int,input().split())
  temp[i]=[i,p,y]
  tempn.append(p)
temp.sort(key=lambda x: x[2])  
temp.sort(key=lambda x: x[1])  
j=1
tempn=list(set(tempn))
tempn.sort()
k=0
n=tempn[k]
for i in range(0,m):
  if temp[i][1]==n:
    tempa=str(n)
    tempj=str(j)
    temp[i][1]=tempa.zfill(6)
    temp[i][2]=tempj.zfill(6)
    j=j+1
  else:
    k=k+1
    if k>=len(tempn):
      break
    n=tempn[k]
    j=1
    tempa=str(n)
    tempj=str(j)
    temp[i][1]=tempa.zfill(6)
    temp[i][2]=tempj.zfill(6)
    j=j+1
temp.sort(key=lambda x: x[0])
for i in range(0,m):
  print(temp[i][1]+temp[i][2])

Solution (Python):
n,m=map(int,input().split())
x=[[] for _ in range(n+1)]
ans=[-1]*m
for i in range(m):
    p,y=map(int,input().split())
    x[p].append([y,i])

for p in range(1,n+1):
    tmp=sorted(x[p])
    for j in range(len(tmp)):
        ans[tmp[j][1]]=str(p).zfill(6)+str(j+1).zfill(6)

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

Solution (Python):
n,m=map(int,input().split())
x=[[] for _ in range(n+1)]
ans=[-1]*m
for i in range(m):
    p,y=map(int,input().split())
    x[p].append([y,i])

for p in range(1,n+1):
    x[p].sort()
    for j in range(len(x[p])):
        ans[x[p][j][1]]=str(p).zfill(6)+str(j+1).zfill(6)

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

Solution (Python):
_,m=map(int,input().split())
ta=[list(map(int,input().split()))+[i] for i in range(m)]
a=sorted(ta)
ans=[-1]*m
j=0
for i in range(m):
    if j!=a[i][0]:
        j=a[i][0]
        k=1
    p=str(a[i][0]).zfill(6)+str(k).zfill(6)
    ans[a[i][2]]=p
    k+=1

a.sort()
for i in range(m):
    print(ans[i])

Solution (Python):
N,M=list(map(int,input().split()))
import sys
l=[list(map(int,_.rstrip().split())) for _ in sys.stdin.readlines()]
n=sorted(l,key=lambda x:x[1])
dic={}
mem=[0]*N
for i,j in n:
   mem[i-1]+=1
   dic[i,j]=str(i).zfill(6)+str(mem[i-1]).zfill(6)
for i,j in l:
   print(dic[i,j])

Solution (Python):

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from collections import defaultdict
n,m = map(int, input().split())
p = defaultdict(list)
ps = [None] * m
ys = [None] * m
for i in range(m):
    pp,yy = map(int, input().split())
    p[pp].append((i,yy))
    ps[i] = pp
    ys[i] = yy
d = {}
for k,v in p.items():
    tmp = sorted(v, key=lambda x: x[1])
    d[k] = {j:i+1 for i,(j,y) in enumerate(tmp)}

for i in range(m):
    print(str(ps[i]).rjust(6,"0")+str(d[ps[i]][i]).rjust(6, "0"))

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

py.sort()

old=py[0][0]
py[0].append(1)
order=1
for i in range(1,m):
    now=py[i][0]
    if now==old:
        order+=1
    else:
        order=1
    py[i].append(order)
    old=now

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

for pypy in py:
    ans = str(pypy[0]).zfill(6) + str(pypy[3]).zfill(6)
    print(ans)

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

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

last_P = -1
counter = 1
for P, Y in sorted(PYs):
    if last_P == P:
        counter += 1
    else:
        last_P = P
        counter = 1
    d[(P, Y)] = "{0:0>6}{1:0>6}".format(P, counter)
    
for t in PYs:
    print(d[t])

Solution (Python):
DEBUG = False

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

if DEBUG:
    print(iPY)

sorted_iPY = sorted(iPY, key=lambda x: x[2])

if DEBUG:
    print(sorted_iPY)

counts = {}

for n, ipy in enumerate(sorted_iPY):
    i, p, y = ipy
    if p not in counts:
        counts[p] = 1
    else:
        counts[p] += 1
    if DEBUG:
        print(counts)
    sorted_iPY[n].append("{:0=6}".format(p) +  "{:0=6}".format(counts[p]))

if DEBUG:
    print(sorted_iPY)

restored_iPY = sorted(iPY, key=lambda x: x[0])

if DEBUG:
    print(restored_iPY)

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

Solution (Python):
import sys

readline = sys.stdin.readline

def flatten_2dim(array):
    return [item for sublist in array for item in sublist]

def main():
    N, M = map(int, readline().split())
    number = [[] for _ in range(N)]
    for i in range(M):
        p, y = map(int, readline().split())
        p -= 1
        number[p].append([p+1, y, i])
    for pnumber in number:
        if len(pnumber)==0:
            continue
        pnumber.sort(key=lambda x: x[1])
        pbase = str(pnumber[0][0])
        numleft = '0'*(6-len(pbase)) + pbase
        cnt = 1
        for tmp in pnumber:
            numbase = str(cnt)
            numright = '0'*(6-len(numbase)) + numbase
            base = numleft+numright
            tmp.append(base)
            cnt += 1
    fnumber = flatten_2dim(number)
    fnumber.sort(key=lambda x: x[2])

    for tmp in fnumber:
        print(tmp[3])

if __name__ == "__main__":
    main()

Solution (Python):


import bisect
import sys
readline = sys.stdin.readline

base = 1000000000000
N, M = map(int, readline().split())
PY = []
T = [[] for _ in range(N+1)]

for i in range(M):
    P, Y = map(int, readline().split())
    PY.append((P, Y))

PY_sorted = sorted(PY, key=lambda x: x[1])
PY_sorted = sorted(PY_sorted, key=lambda x: x[0])

for i in range(M):
    T[PY_sorted[i][0]].append(PY_sorted[i][1])

for i in range(M):
    n = bisect.bisect_right(T[PY[i][0]], PY[i][1])
    print(str(base + PY[i][0]*1000000 + n)[1:])

Solution (Python):
n, m = map(int, input().split())
cnt = [[] for _ in range(n)]
for i in range(m):
    p, y = map(int, input().split())
    cnt[p-1].append((y, i))
ans = [None]*m
for p, li in enumerate(cnt):
    if not li:
        continue
    li.sort()
    for j, (y, i) in enumerate(li):
        ans[i] = '{:06d}{:06d}'.format(p+1, j+1)
print('\n'.join(ans))

Solution (Python):
N, M = map(int, input().split())
PYi = [list(map(int, input().split()))+[i] for i in range(M)]
PYi.sort()
ans = ['']*M
counter = {}

for v in PYi:
    P, Y, i = v

    if not P in counter:
        counter[P] = 1
    else:
        counter[P] += 1

    tmp = ''
    tmp += str(P).zfill(6)
    tmp += str(counter[P]).zfill(6)
    ans[i] = tmp

for v in ans:
    print(v)

Solution (Python):






import sys
input = sys.stdin.readline

import math
import bisect
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def Yes(): print("Yes"); return
def No(): print("No"); return


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count





N,M = inputIL()
data = []
for i in range(M):
  data.append([i] + inputIL())

dic = {p:[] for p in range(N)}

for i,p,y in data:
  dic[p-1].append((y,i))

ans = []
for p in dic:
  dic[p].sort()
  for k in range(len(dic[p])):
    y,i = dic[p][k]
    ans.append((i,(p+1)*1000000+k+1))
    
ans.sort()   
for i,id in ans:
  print("{:012}".format(id))

Solution (Python):
import numpy as np
import bisect
n, m = map(int, input().split())


data = [0] * m
for i in range(m):
    
    p, y = map(int, input().split())
    temp = [p, y, i, 0]
    data[i] = temp






data1 = sorted(data)
dArray = np.array(data1)
pref = dArray[:, 0]

setPref = set(pref)
numPref = len(setPref)


for i in setPref:
    left = bisect.bisect_left(pref, i)
    right = bisect.bisect_right(pref, i) - 1
    tar = left - right + 1
    id = 1
    for j in range(left, right + 1):
        
        pre = '%06d' % (i)
        post = '%06d' % (id)
        dArray[j, 3] = pre + post
        id += 1

ansList = dArray[dArray[:, 2, ].argsort(), :]
for i in range(m):
    ans = ansList[i, 3]
    ansS = '{:0>12}'.format(ans)
    print(ansS)

Solution (Python):
import itertools
import math
import fractions
import functools
import copy
n, m = map(int, input().split())
pyi = []
for i in range(m):
    p,y= map(int, input().split())
    pyi.append([p,y,i])

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

ken = 0
for i in range(m):
    if pyi[i][0] != ken:
        ken = pyi[i][0]
        x = 1
    else:
        x += 1
    pyi[i].append(x)


pyi.sort(key=lambda x: x[2])
for i in range(m):
    print(str(pyi[i][0]).zfill(6)+str(pyi[i][3]).zfill(6))

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True




def warshall_floyd(d, n):
    
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d



def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)






def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret



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

for i in range(m):
    index = bisect_left(x[p[i] - 1], y[i])
    ans = str(p[i]).zfill(6) + str(index + 1).zfill(6)
    print(ans)

Solution (Python):
N, M = map(int, input().split())
ss = [[] for _ in range(N)]
for i in range(M):
    p, y = map(int, input().split())
    ss[p-1].append((i, y))
ls = [''] * M
for p in range(N):
    s = ss[p]
    s.sort(key=lambda g: g[1])
    for d, (i, y) in enumerate(s):
        ps = str(p+1).zfill(6)
        ds = str(d+1).zfill(6)
        ls[i] = ps+ds
for l in ls:
    print(l)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


N, M = getNM()
city = []



pref = [1] * (N + 1)
for i in range(M):
    
    
    p, y = getNM()
    
    city.append([i + 1, p, y])

city = sorted(city, key=lambda i: i[2])

ans = []
for i, p, y in city:
    np = len(str(p))
    uppercode = '0' * (6 - np) + str(p)

    
    order = pref[p]
    yp = len(str(order))
    undercode = '0' * (6 - yp) + str(order)
    
    pref[p] += 1
    ans.append([i, uppercode + undercode])

ans = sorted(ans, key=lambda i: i[0])
for i in ans:
    print(i[1])

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18


n, m = Is()
PY = []
for i in range(m):
    p, y = Is()
    PY.append((i, p, y))
PY.sort(key=itemgetter(1, 2))
ANS = [[0]*2 for _ in range(m)]
tmp = -1
count = -1
for i, p, y in PY:
    if tmp != p:
        count = 1
        tmp = p
    else:
        count += 1
    ANS[i][0] = p
    ANS[i][1] = count
for a, b, in ANS:
    print("{}{}".format(str(a).zfill(6), str(b).zfill(6)))

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

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

result = [1] * M
citys = [[] for _ in range(N)]

for i, n in enumerate(P):
    citys[n-1].append([Y[i], i])

for c in citys:
    
    c.sort()
    for i, k in enumerate(c):
        result[k[1]] += i



for x, y in zip(P, result):
    x, y = str(x), str(y)
    print('0'*(6-len(x))+x+'0'*(6-len(y))+y)

Solution (Python):
n, m = map(int, input().split())
py = []
for _ in range(m):
    p, y = map(int, input().split())
    py.append((p, y))
prefs = [[] for _ in range(n)]
for ind, (p, y) in enumerate(py):
    prefs[p-1].append((y, ind))
ans = [None]*m
for p, i in enumerate(prefs):
    i.sort()
    for j, (_, ind) in enumerate(i):
        ans[ind] = str(p+1).zfill(6)+str(j+1).zfill(6)
for i in ans:
    print(i)

Solution (Python):
import copy
n, m = map(int, input().split())
p = [list(map(int, input().split())) for i in range(m)]
dic = {}
q = copy.copy(p)
q.sort(key=lambda x: x[1])
cnt = [0 for i in range(n+1)]
for i in range(m):
    cnt[q[i][0]] += 1
    dic[q[i][1]] = cnt[q[i][0]]
for i in range(m):
    print(str(p[i][0]).zfill(6)+str(dic[p[i][1]]).zfill(6))

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

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

ls = [''] * M
for p in range(N):
    s = ss[p]
    s.sort(key=lambda g: g[1])
    for d, (i, y) in enumerate(s):
        ps = str(p+1).zfill(6)
        ds = str(d+1).zfill(6)
        ls[i] = ps+ds

for l in ls:
    print(l)

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

spy = sorted(py, key=lambda x: x[1])
ids = {}
maxy = [1] * (n + 1)
for p, y in spy:
    ids[(p, y)] = maxy[p]
    maxy[p] += 1
for p, y in py:
    print('{:0=6}{:0=6}'.format(p, ids[p,y]))

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

spy = sorted(py, key=lambda x: x[1])
ids = {}
maxy = [1] * (n + 1)
for p, y in spy:
    ids[(p, y)] = maxy[p]
    maxy[p] += 1
for p, y in py:
    print('{:0=6}'.format(p) + '{:0=6}'.format(ids[p,y]))

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

ol = []


def zeros(x):
    xt = str(x)
    k = 6 - len(xt)
    return "0" * k + xt

for i in range(M):
    p,y = map(int, input().split())
    ol.append((p,y))

l = sorted(ol)


res = {}

t=0
for i in range(M):
    if l[i][0] == l[i-1][0]:
        t+=1
    else:
        t=1
    res[l[i]] = zeros(l[i][0])+ zeros(t)

for i in ol:
    print(res[i])

Solution (Python):
from bisect import bisect_left
from collections import defaultdict
n, m = map(int, input().split())
py = []
dict_p = defaultdict(list)
for i in range(m):
    p, y = map(int, input().split())
    py.append((p, y))
    dict_p[p].append(y)

for values in dict_p.values():
    values.sort()

for p, y in py:
    x = bisect_left(dict_p[p], y)
    ans = "{:006}".format(p) + "{:006}".format(x+1)
    print(ans)

Solution (Python):
n,m = map(int,input().split())
p = {str(i):[] for i in range(1,n+1)}
l = []
for i in range(m):
    t = input().split()
    l.append((t[0],int(t[1])))
    p[t[0]].append(int(t[1]))
for i in range(1,n+1):
    i = str(i)
    p[i].sort()
    d = {}
    for j in range(len(p[i])):
        d[p[i][j]] = str(i).zfill(6)+str(j+1).zfill(6)
    p[i]=d
for t in l:
    print(p[t[0]][t[1]])

Solution (Python):
from collections import defaultdict
from bisect import bisect_left

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

city_list = []
city_list_by_pref = defaultdict(list)
for i in range(m):
    p, y = map(int, input().split())
    city_list.append((p, y))
    city_list_by_pref[p].append(y)

for v in city_list_by_pref.values():
    v.sort()

for p, y in city_list:
    x = bisect_left(city_list_by_pref[p], y)
    print('{:06}{:06}'.format(p, x+1))

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

Pref = [[]for _ in range(N)]

City = []

for _ in range(M):
    prefecture, year = map(int, input().split())
    prefecture -= 1
    Pref[prefecture].append([_, year])

for i in range(N):
    Pref[i].sort(key=lambda x: x[1])
    for j in range(len(Pref[i])):
        s1 = str(i+1).zfill(6)
        s2 = str(j+1).zfill(6)
        Pref[i][j].append(s1+s2)
        City.append(Pref[i][j])
City.sort()
for a, b, c in City:
    print(c)

Solution (Python):
from collections import defaultdict


def main():
    N, M = list(map(int, input().split(' ')))
    pref = defaultdict(list)
    for c in range(M):
        p, y = list(map(int, input().split(' ')))
        p -= 1
        pref[p].append((c, y))
    nums = ['' for _ in range(M)]
    for p in range(N):
        pref[p].sort(key=lambda a: a[1])
        for x, city in enumerate(pref[p]):
            nums[city[0]] = '{:06}{:06}'.format(p + 1, x + 1)
    for n in nums:
        print(n)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
py = [list(map(int,input().split()))for i in range(m)]
pypy = py[::]
from operator import itemgetter
import bisect
py.sort(key = itemgetter(1))
dic = {}
for i in range(m):
    if py[i][0] not in dic:
        dic[py[i][0]] = []
    dic[py[i][0]].append(py[i][1])


anslist = [0]*m

for i in range(m):
    anslist[i] = str(pypy[i][0]).zfill(6) + str(bisect.bisect_left(dic[pypy[i][0]],pypy[i][1]) + 1).zfill(6)

for ans in anslist:
    print(ans)

Solution (Python):
from collections import Counter
n, m = map(int, input().split())
py = []
for i in range(m):
    t = list(map(int, input().split()))
    py.append(t+[i])

c = Counter([])
py.sort(key=lambda x: x[1])

ans = []
for p, y, i in py:
    c[p]+=1
    ans.append((i, str(p).zfill(6)+str(c[p]).zfill(6)))
ans.sort()
for i, a in ans:
    print(a)

Solution (Python):
def main():
    n, m = map(int, input().split())
    p, y = [0 for i in range(m)], [0 for i in range(m)]
    for i in range(m):
        p[i], y[i] = map(int, input().split())
    ans = solve(n, m, p, y)


def solve(n, m, p, y):
    kens = [[] for i in range(n + 1)]
    for i in range(m):
        kens[p[i]].append(y[i])
    for ken in kens:
        ken.sort()
    
    
    ids = [{} for i in range(n + 1)]
    for indken, ken in enumerate(kens):
        for indsi, yi in enumerate(ken, 1):
            ids[indken][yi] = str(indken).zfill(6) + str(indsi).zfill(6)
    
    
    for i in range(m):
        print(ids[p[i]][y[i]])

    return

if __name__ == '__main__':
    main()

Solution (Python):
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def cmp_vec(u, v):
    if u[1] < v[1]:
        return -1
    elif u[1] == v[1]:
        if u[2] < v[2]:
            return -1
        elif u[2] == v[2]:
            return 0
        else:
            return 1
    else:
        return 1


def main():
    
    n, m = map(int, input().strip().split())
    l = []
    for i in range(m):
        p, y = map(int, input().strip().split())
        p = str(p).zfill(6)
        l.append(list((i, p, y)))
    l.sort(key=functools.cmp_to_key(cmp_vec))

    
    cntr = 1
    for i in range(m):
        if i > 0 and l[i][1] != l[i-1][1]:
            cntr = 1
        l[i][2] = str(cntr).zfill(6)
        cntr += 1

    
    l.sort(key=lambda x: x[0])
    for xx in l:
        print(xx[1],end="")
        print(xx[2])


if __name__ == '__main__':
    main()

Solution (Python):
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def cmp_vec(u,v):   
    if u[1] < v[1]:
        return  1
    elif u[1] == v[1]:
        if u[2] < v[2]:
            return 1
        elif u[2]==v[2]:
            return 0
        else:
            return -1
    else:
        return -1

def main():
    
    n,m = map(int,input().strip().split())  
    l=[]
    for i in range(m):
        p,y = map(int,input().strip().split()) 
        p = str(p).zfill(6) 
        l.append(list((i,p,y)))
    l.sort(key=functools.cmp_to_key(cmp_vec),reverse = True)

    
    eprint('l (i,p,y)',end=':\n')
    eprint(l)

    
    cntr=1
    for i in range(m):
        if i>0 and l[i][1] != l[i-1][1]:
            cntr=1
        l[i][2] = str(cntr).zfill(6)
        cntr+=1

    
    l.sort(key=lambda x:x[0])
    for xx in l:
        print(xx[1]+xx[2])
    
    
    
    
    
    

if __name__ == '__main__':
    main()

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

Ps = [[] for i in range(N)]
Cs = []
for i in range(M):
    p, y = map(int, input().split())
    p -= 1
    Cs.append([p, 0])
    Ps[p].append([i, y])

for p in Ps:
    p = sorted(p, key=lambda city: city[1])
    for i in range(len(p)):
        Cs[p[i][0]][1] = i+1

for i in range(M):
    print('{:0=6}{:0=6}'.format(Cs[i][0]+1, Cs[i][1]))

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

pref = {}
for p,y in info:
    if not p in pref: pref[p] = [y]
    else: pref[p].append(y)

id = {}
for p,years in pref.items():
    years.sort()
    id[p] = {}
    for i,y in enumerate(years):
        id[p][y] = str(p).zfill(6) + str(i+1).zfill(6)

for p,y in info:
    print(id[p][y])

Solution (Python):
import numpy as np
N, M = map(int, input().split())
cities = np.array(tuple(tuple(map(int, input().split())) for _ in range(M)), dtype=np.int64)
arg = np.argsort(cities[:, 0] * 10 ** 10 + cities[:, 1])
pref = 0
ans = []
for p, y in cities[arg]:
    if p != pref:
        pref = p
        count = 1
    ans.append("{:06d}{:06d}".format(p, count))
    count += 1
for i in np.argsort(arg):
    print(ans[i])

Solution (Python):
def bisect(seq, target):
  left = -1    
  right = len(seq)    
  while right - left > 1:
    mid = (left + right) // 2
    if seq[mid] < target:
      left = mid
    else:
      right = mid
  return right



n, m = map(int, input().split())
L = [[] for _ in range(n + 1)]
pairs = [list(map(int, input().split())) for _ in range(m)]
for p, y in pairs:
  L[p].append(y)

for i in range(1, n + 1):
  L[i].sort()


for p, y in pairs:
  left_id = "{:0>6}".format(p)
  right_id = "{:0>6}".format(bisect(L[p], y) + 1)
  print(left_id + right_id)