#K15981. Contact Sorting Problem

    ID: 24477 Type: Default 1000ms 256MiB

Contact Sorting Problem

Contact Sorting Problem

You are given a list of contacts. Each contact consists of a first name and a last name. Your task is to sort the contacts primarily by first name in lexicographical order, and secondarily by last name in lexicographical order. If two contacts have identical names, their original order is preserved.

The sorting criterion can be mathematically expressed as follows:

\( (a, b) < (c, d) \) if and only if \( a < c \) or (\( a = c \) and \( b < d \)).

Your solution should read input from standard input (stdin) and output the sorted contacts to standard output (stdout) with each contact on a separate line.

inputFormat

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

  • The first line contains an integer \( n \) representing the number of contacts.
  • The next \( n \) lines each contain two space-separated strings representing the first name and last name of a contact.

outputFormat

Output the sorted contacts to stdout. Each line should contain the first name and last name separated by a space.

## sample
5
alice smith
bob jones
alice cooper
claire white
bob brown
alice cooper

alice smith bob brown bob jones claire white

</p>