#K88372. Ordering Jousting Contests

    ID: 37294 Type: Default 1000ms 256MiB

Ordering Jousting Contests

Ordering Jousting Contests

In this problem, you are given a list of jousting contests. Each contest is represented by a pair of integers ((a, b)) where (a) is the ID of the winning knight and (b) is the ID of the losing knight. Your task is to reorder the list of contests such that the contests are sorted in ascending order by the winner's ID, and in the case of a tie, by the loser's ID.

Formally, given a sequence of contests (C = [(a_1, b_1), (a_2, b_2), \dots, (a_n, b_n)]), you should output a sequence (C') that is the sorted version of (C) where for any two contests ((a_i, b_i)) and ((a_j, b_j)) with (i < j), either (a_i < a_j) or (a_i = a_j) and (b_i \le b_j).

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains a single integer (n), the number of contests.
  • The following (n) lines each contain two integers (a) and (b) separated by a space, representing a contest where (a) is the winner's ID and (b) is the loser's ID.

outputFormat

Print the ordered list of contests to standard output (stdout). Each contest should be printed on a separate line as two space-separated integers.## sample

4
2 3
4 2
3 1
5 4
2 3

3 1 4 2 5 4

</p>