P14150 [Kattis] Fantasydraft
普及
题目描述

In fantasy hockey, there are $n$ team owners that each selects $k$ hockey players. To determine which owner gets which players, the owners hold a draft.

The draft proceeds as follows: the first owner may select any player, then the second owner can select any player except the player taken first and so on. In general, the current owner may take any player that has not been taken previously. Once all owners have selected one player, they repeat this process until all owners have selected $k$ players. No player may be selected by multiple teams.

Initially, all players are given a ranking based on how well they played in the previous year. However, the owners may not agree with this order. For example, the first owner may believe that the player which was ranked third in the previous year is the best player and would prefer to take them.

Each owner has a preference list. On their turn, the owner selects the player that is the highest available player on their own preference list. If all players on their preference list are taken, then they resort to using the ordering from the previous year.

Given the preference list of each owner and the rankings from the previous year, which players did each owner get?

-----Input-----
The first line of the input contains two integers $n$ ($1 \leq n \leq 60$), the number of owners, and $k$ ($1 \leq k \leq 1000$), the size of each team.

The next $n$ lines contain owners’ preferences in the order of drafting. Each line starts with an integer $q_ i$ ($0 \leq q_ i \leq 1500$), the size of the $i^\textrm$ owners’ preference list. $q_ i$ names follow, separated by spaces, in order of $i^\textrm$ owner’s preference. No name appears more than once in the $i^\textrm$ owners’ list.

The next line contains a single integer $p$ ($n \cdot k \leq p \leq 65000$), indicating the number of players in the draft.

The next $p$ lines each contain a single name, they are ordered by their previous year’s ranking. Each player name is unique and comprised of at most $12$ letters of English alphabet.

The names in owners’ preference lists are guaranteed to appear in the player list.

-----Output-----
Display $n$ lines. The $i^\textrm$ of which contains the $k$ names of the players that were selected by the $i^\textrm$ owner. The $n$ teams should be in the original order of owners and players should be listed in the order in which they were drafted following the rules above.

-----Examples-----
Sample Input:
2 2
0
0
6
Shoresy
Jonesy
Reilly
Sholtzy
Fisky
Yorkie
Sample Output:
Shoresy Reilly
Jonesy Sholtzy

输入格式

见题面

输出格式

见题面

样例输入
2 2 0 0 6 Shoresy Jonesy Reilly Sholtzy Fisky Yorkie
样例输出
Shoresy Reilly Jonesy Sholtzy