IOI 2017 Logo Vote

Each voter ranks up to three logos worth 3, 2, 1 points; find all logos winning on score, then first votes, then second votes.

Easy3ImplementationSortingHash mapInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Iran hosts the International Olympiad in Informatics (IOI) 2017. To pick the IOI 2017 logo, the organizing committee held an open call for submissions. Many logos arrived in a short time, which was no surprise because the young generation in Iran takes an active part in any national event. In the first round, professional graphic designers judged the submissions and selected the ones artistically capable of being the IOI 2017 logo for the second round.

The selected logos are now presented to the members of the organizing committee for a vote. The voting system is a little complicated. Each member can vote for at most three different logos in some order. The first, second and third choices of each member are awarded 3, 2 and 1 points. The score of a logo is the total number of points it receives from all members. The logo with the highest score wins. If several logos have the same score, the one with more first votes wins. If several logos still have the same score and the same number of first votes, the one with more second votes wins. If a tie remains after that, all of the tied logos win. Given the voting information, find every winning logo.

Input

The input contains several test cases. The first line of each test case contains a positive integer nn, the number of voters (1n1001 \le n \le 100). The ii-th of the next nn lines starts with an integer did_i (1di31 \le d_i \le 3), the number of logos chosen by the ii-th voter, followed by did_i different logo IDs listed from left to right in that voter's order of preference. Each logo ID is a positive integer not exceeding 10610^6. All integers on a line are separated by a single space. The input ends with a line containing 00, which must not be processed.

Output

For each test case, print one line with the IDs of the winning logos in increasing order. Separate the IDs on a line with a single space.