Buggy python submission (verdict: Time Limit Exceeded):
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

N,M=map(int, input().split())
city = defaultdict(list)
p = [0] * M
y = [0] * M

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

city_sorted = defaultdict(list)
year = defaultdict(list)
for key,v in city.items():
    city_sorted[key] = sorted(v)
    for X in city_sorted.values():
        for i,x in enumerate(X):
            year[x] = i+1

for a in range(M):
    print(str(p[a]).zfill(6)+str(year[y[a]]).zfill(6))

Fixed submission:
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

N,M=map(int, input().split())
city = defaultdict(list)
p = [0] * M
y = [0] * M

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

year = defaultdict(list)
for key,v in city.items():
    for i,x in enumerate(sorted(v)):
        year[x] = i+1

for a in range(M):
    print(str(p[a]).zfill(6)+str(year[y[a]]).zfill(6))

Buggy python submission (verdict: Time Limit Exceeded):
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

N,M=map(int, input().split())
city = defaultdict(list)
p = [0] * M
y = [0] * M

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

city_sorted = defaultdict(list)
for key,v in city.items():
    city_sorted[key] = sorted(v)

for a in range(M):
    for (i,x) in enumerate(city_sorted[p[a]]):
        if (y[a] == x):
            print(str(p[a]).zfill(6)+str(i+1).zfill(6))
            break

Fixed submission:
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

N,M=map(int, input().split())
city = defaultdict(list)
p = [0] * M
y = [0] * M

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

year = defaultdict(list)
for key,v in city.items():
    for i,x in enumerate(sorted(v)):
        year[x] = i+1

for a in range(M):
    print(str(p[a]).zfill(6)+str(year[y[a]]).zfill(6))

Buggy python submission (verdict: Time Limit Exceeded):
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

N,M=map(int, input().split())
city = defaultdict(list)
p = [0] * M
y = [0] * M

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

city_sorted = defaultdict(list)
for key,v in city.items():
    city_sorted[key] = sorted(v)

for a in range(M):
    key = p[a]
    for (i,x) in enumerate(city_sorted[key]):
        if (y[a] == x):
            print(str(key).zfill(6)+str(i+1).zfill(6))

Fixed submission:
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

N,M=map(int, input().split())
city = defaultdict(list)
p = [0] * M
y = [0] * M

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

year = defaultdict(list)
for key,v in city.items():
    for i,x in enumerate(sorted(v)):
        year[x] = i+1

for a in range(M):
    print(str(p[a]).zfill(6)+str(year[y[a]]).zfill(6))