Best Buddies
InterviewTime limit2sMemory limit256 MB
Sort all participants by last name then first name, split the order into consecutive triples, and print each queried person's two teammates.
Problem
You signed up for a yearly collegiate programming contest. Unlike previous years, participants do not pick their own team mates. Instead, every participant is placed in a line in alphabetical order, and the line is cut into blocks of three: the first three participants form team 1, the next three form team 2, and so on. The number of participants is a multiple of 3, so every team has exactly 3 members.
Every participant has one first name and one last name, both written with the lower case letters a to z. No two participants share the same first name and last name. The alphabetical order compares last names in ascending lexicographic order first, and compares first names only when the last names are equal. So john smith comes after carla smith, but before bob taylor.
Given the name of a participant, print the names of that participant's two team mates.
Input
The first line contains the number of participants . (, and is a multiple of 3)
Each of the next lines contains the first name and the last name of one participant, separated by a space. Both names are strings of lower case English letters with length at least 2 and at most 16.
The next line contains the number of queries . ()
Each of the next lines contains the first name and the last name of one queried participant. Every queried participant appears in the list.
Output
For each query, print the two team mates of the queried participant in the alphabetical order described above, one participant per line. On each line, print the first name and the last name separated by a single space.