Australian Voting

Time limit1sMemory limit128 MB

Problem

In Australian voting, each voter ranks all candidates in order of preference. Counting proceeds in rounds:

  1. First, count only the first-choice vote on every ballot.
  2. If some candidate has more than 50% of the votes, that candidate is elected.
  3. Otherwise, every candidate tied for the fewest votes is eliminated. Each ballot cast for an eliminated candidate is transferred to its highest-ranked candidate who has not yet been eliminated.
  4. Repeat until one candidate has more than 50% of the votes, or until all remaining candidates have the same number of votes (a tie).

Input

The first line contains an integer $n$ ($1 \le n \le 20$), the number of candidates. Each of the next $n$ lines gives a candidate's name; a name may be up to 80 characters long and may contain any printable characters, including spaces. Up to 1000 ballots follow, one per line. Each ballot lists the integers $1$ through $n$ in some order: the first integer is the voter's first choice, the second integer the second choice, and so on.

Output

Print a single line with the name of the winner. If the process ends in a tie, print the name of every tied candidate on its own line, in the same order the candidates were given in the input.