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 MBIran 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.
The input contains several test cases. The first line of each test case contains a positive integer n, the number of voters (1≤n≤100). The i-th of the next n lines starts with an integer di (1≤di≤3), the number of logos chosen by the i-th voter, followed by di different logo IDs listed from left to right in that voter's order of preference. Each logo ID is a positive integer not exceeding 106. All integers on a line are separated by a single space. The input ends with a line containing 0, which must not be processed.
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.