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 in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;


public class Main {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);

        int n = in.nextInt();
        int m = in.nextInt();
        HashMap<Integer,List<Integer>> list = new HashMap<>();
        LinkedList<City> q = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            int p = in.nextInt();
            int y = in.nextInt();
            q.addLast(new City(p,y));
            List<Integer> cities;
            if(list.containsKey(p)) {
                cities = list.get(p);
            }
            else {
                cities = new ArrayList<>();
            }
            cities.add(y);
            list.put(p,cities);
        }
        for (Map.Entry<Integer,List<Integer>> mp : list.entrySet()) {
            Collections.sort(mp.getValue());
        }

        while (!q.isEmpty()) {
            City current = q.pollFirst();
            int p = current.getP();
            int y = current.getY();
            List<Integer> l = list.get(p);
            int index = Collections.binarySearch(l,y);
            String id = constructId(p,index + 1);
            out.println(id);
        }

        out.flush();
    }
    static String constructId(int p,int index) {
        String ps = Integer.toString(p);
        String is = Integer.toString(index);
        int pl = ps.length();
        int il = is.length();
        if(pl < 6) {
            int diff = 6 - pl;
            StringBuilder sb = new StringBuilder();
            while (diff-->0) {
                sb.append("0");
            }
            sb.append(ps);
            ps = sb.toString();
        }
        if(il < 6) {
            int diff = 6 - il;
            StringBuilder sb = new StringBuilder();
            while (diff-->0) {
                sb.append("0");
            }
            sb.append(is);
            is = sb.toString();
        }
        return ps+is;
    }

    private static List<Integer> divisors(int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i*i<=n; i++) {
            if(n%i==0) {
                list.add(i);
                if(n/i!=i) {
                    list.add(n/i);
                }
            }
        }
        return list;
    }

}
class City {
    int p;
    int y;
    public int getP() {
        return p;
    }
    public int getY() {
        return y;
    }
    public City(int p,int y) {
        this.p = p;
        this.y = y;
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }
    public Integer nextInt() {return Integer.valueOf(next());}
    public Long nextLong() {return  Long.valueOf(next());}
    public Double nextDouble() {return Double.valueOf(next());}

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}