Under construction

Still being built. Things may change

Politics

Interview

Time limit15sMemory limit256 MB

Summary
Order supporters by the listed candidate order, keep original order within each candidate, and group unlisted candidates at the end by first appearance.
Level

Easy3 of 10

Topics
Sorting, Hash map
Solved
No attempts yet

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 nn (1≤n≤10001 \le n \le 1000) and mm (1≤m≤1000001 \le m \le 100000), the number of candidates and the number of supporters. The next nn lines give the candidate names, one per line. Each name is a single word of 1 to 30 capital letters, and the nn candidate names in a test case are all different. The next mm 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.

Examples1

  1. Example 1

    Input
    3 5
    STEVENS
    MICHAELS
    JORDAN
    BOB JORDAN
    JACK STEVENS
    MACK MICHAELS
    BILL JORDAN
    CHRIS MATTHEWS
    1 5
    FRED
    SAM FRED
    ARTHUR GEORGE
    DANIEL HERBERT
    MARK GEORGE
    MIKE HERBERT
    0 0
    
    Expected output
    JACK
    MACK
    BOB
    BILL
    CHRIS
    SAM
    ARTHUR
    MARK
    DANIEL
    MIKE