Given each driver's finishing place in every race, compute total points under several scoring systems and print the champion(s), breaking ties by listing all joint champions.
Easy3ImplementationArraySortingInterviewNo attempts yetTime limit2sMemory limit512 MBA Formula 1 season consists of a series of races called Grands Prix, organized by the Federation Internationale de l'Automobile (FIA). Each Grand Prix awards points to the drivers according to the order in which they finish. At the end of the season the driver with the largest point total becomes World Drivers' Champion.
The organizers change the rules often to make the racing tighter. One rule that changed for the 2010 season is how points are handed out at each Grand Prix. From 2003 the first eight finishers scored points according to this table.
| Place | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Points | 10 | 8 | 6 | 5 | 4 | 3 | 2 | 1 |
So the winner scored 10 points, the runner-up scored 8 points, and the rest followed the same pattern.
From the 2010 season the first ten finishers score points according to this table.
| Place | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Points | 25 | 18 | 15 | 12 | 10 | 8 | 6 | 4 | 2 | 1 |
The new scale started a long argument about which past championships would have ended differently under it. In 2008 the final gap between Lewis Hamilton and Felipe Massa was a single point, so would Hamilton still have been champion? To settle the argument the FIA decided to prepare a program that reads every race result of a season together with several points systems and reports the World Drivers' Champion under each system.
The input contains several test cases.
The first line of a test case contains the number of Grands Prix G (1≤G≤100) and the number of drivers P (1≤P≤100), separated by a space. Drivers are identified by the integers 1 through P. Each of the next G lines gives the result of one race and contains P integers separated by spaces. The i-th number on a line is the place in which driver i finished that race.
The next line contains the number of points systems S (1≤S≤10). Each of the following S lines describes one points system. The line starts with the last scoring place K (1≤K≤P), followed by K integers k1,k2,…,kK (1≤ki≤100) separated by spaces. The first integer is the score for first place, the second integer is the score for second place, and the rest follow the same pattern. A driver who finishes worse than place K scores nothing.
The line after the last test case contains two zeros separated by a space.
For each points system in the input, print one line with the identifier of the World Drivers' Champion.
If there are two or more champions, that is, if a tie makes them joint champions, print every champion identifier on that line in increasing order, separated by single spaces.