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 Kotlin (1.0.0) (judge verdict: Accepted)

@file:Suppress("DuplicatedCode")

import java.io.IOException
import java.io.InputStream
import java.io.PrintWriter
import java.util.NoSuchElementException
import kotlin.comparisons.compareBy

fun main(args: Array<String>) {
    PrintWriter(System.out).use { out ->
        C.solve(C.FastScanner(), out)
        out.flush()
    }
}

private fun C.solve(sc: C.FastScanner, out: PrintWriter) {
    val n = sc.nextInt()
    val m = sc.nextInt()
    val ans = Array(m) {
        val arr = IntArray(2)
        arr[0] = sc.nextInt()
        arr[1] = sc.nextInt()
        arr
    }

    val sorted = ans.copyOf()

    sorted.sortWith(compareBy({ it[0] }, { it[1] }))

    var prev = sorted[0][0]
    var count = 1
    sorted.forEach {
        if (prev == it[0]) {
            it[1] = count++
        } else {
            prev = it[0]
            it[1] = 1
            count = 2
        }
    }

    ans.forEach {
        out.println(String.format("%06d%06d", it[0], it[1]))
    }
}

@Suppress("MemberVisibilityCanBePrivate")
private object C {
    private const val zeroCode = '0'.toInt()
    private const val nineCode = '9'.toInt()

    class FastScanner {
        private val `in`: InputStream = System.`in`
        private val buffer = ByteArray(2048)
        private var ptr = 0
        private var buflen = 0

        private fun hasNextByte(): Boolean {
            if (ptr < buflen) {
                return true
            } else {
                ptr = 0
                try {
                    buflen = `in`.read(buffer)
                } catch (e: IOException) {
                    e.printStackTrace()
                }
                if (buflen <= 0) {
                    return false
                }
            }
            return true
        }

        private fun readByte(): Byte = if (hasNextByte()) buffer[ptr++] else -1

        private fun readByteAsInt(): Int = if (hasNextByte()) buffer[ptr++].toInt() else -1

        operator fun hasNext(): Boolean {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++
            return hasNextByte()
        }

        operator fun next(): String {
            if (!hasNext()) throw NoSuchElementException()

            return buildString {
                var b = readByteAsInt()

                while (isPrintableChar(b)) {
                    appendCodePoint(b)
                    b = readByteAsInt()
                }
            }
        }

        fun toCharIterator(): CharIterator = object: CharIterator() {
            private var nextByte: Byte = readByte()

            override fun hasNext(): Boolean = isPrintableChar(nextByte)

            override fun nextChar(): Char {
                val res = nextByte
                nextByte = readByte()
                return res.toChar()
            }

        }

        fun nextLong(): Long {
            if (!hasNext()) throw NoSuchElementException()
            var n: Long = 0
            var minus = false
            var b = readByteAsInt()

            if (b == '-'.toInt()) {
                minus = true
                b = readByteAsInt()
            }

            if (b !in zeroCode..nineCode) {
                throw NumberFormatException()
            }

            while (true) {
                if (b in zeroCode..nineCode) {
                    n *= 10
                    n += b - zeroCode.toLong()
                } else if (b == -1 || !isPrintableChar(b)) {
                    return if (minus) -n else n
                } else {
                    throw NumberFormatException()
                }

                b = readByteAsInt()
            }
        }

        fun nextInt(): Int {
            val nl = nextLong()
            if (nl < Int.MIN_VALUE || nl > Int.MAX_VALUE) throw NumberFormatException()
            return nl.toInt()
        }

        fun nextDouble(): Double {
            return next().toDouble()
        }

        fun readIntArray(n: Int): IntArray {
            val arr = IntArray(n)

            for (i in 0 until n) {
                arr[i] = nextInt()
            }

            return arr
        }

        fun readBigInt(capacity: Int): IntArray {
            if (!hasNext()) throw NoSuchElementException()

            val arr = IntArray(capacity)
            var len = 0
            arr[0] = readByteAsInt() - zeroCode
            while (arr[len] in 0..9) {
                len++
                arr[len] = readByteAsInt() - zeroCode
            }

            return arr.sliceArray(0 until len)
        }

        companion object {
            private inline fun isPrintableChar(c: Int): Boolean {
                return c in 33..126
            }

            private inline fun isPrintableChar(c: Byte): Boolean {
                return c in 33..126
            }
        }
    }
}