Politics
InterviewTime limit15sMemory limit256 MB
Order supporters by the listed candidate order, keep original order within each candidate, and group unlisted candidates at the end by first appearance.
Problem
You are helping a political party sort its member list. The party has a list of candidates it wants to support, and it has a list of members, each of whom supports one candidate.
The party wants the member list sorted by the rules below. The candidates come first, in the order the party wants them. After that comes the list of supporters, each with the one candidate they support.
Order the supporters by the candidate they support. When several supporters back the same candidate, keep the order they had in the original list. Some supporters write in a candidate who is not on the party's list. Send those supporters to the end, group them by candidate, and place the groups in the order their candidates first appear in the supporter list. Some candidates may have no supporters at all. They will probably be dropped from the list later, but that is politics.
Input
The input holds several test cases. Each test case begins with a line containing two integers () and (), the number of candidates and the number of supporters. The next lines give the candidate names, one per line. Each name is a single word of 1 to 30 capital letters, and the candidate names in a test case are all different. The next lines describe the supporters, one per line. Each of these lines holds two words separated by a single space, and each word is 1 to 30 capital letters. The first word is the supporter's name and the second is the name of the candidate they support. The input ends with a line holding two zeros.
Output
For each test case, print the supporter names in the requested order, one name per line. Print no spaces, and print no blank line between test cases.