// Arup Guha
// 11/2/2013
// Solution to 2013 South East Regional Division 2 Problem G: Politics

import java.util.*;

public class politics {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCand = stdin.nextInt();
		int numVoters = stdin.nextInt();

		// Go through each case.
		while (numCand != 0) {

			// Store look up table here.
			HashMap<String,Integer> map = new HashMap<String,Integer>();

			// Store numbers.
			for (int i=0; i<numCand; i++) {
				String s = stdin.next();
				map.put(s, i);
			}

			voter[] voters = new voter[numVoters];

			// Read through voters.
			for (int i=0; i<numVoters; i++) {
				String name = stdin.next();
				String cand = stdin.next();

				// Pair voter with candidate number.
				if (map.containsKey(cand))
					voters[i] = new voter(name, i, map.get(cand));

				// Add if not on the list.
				else {
					map.put(cand, numCand);
					voters[i] = new voter(name, i, numCand);
					numCand++;
				}
			}

			// Sort and print.
			Arrays.sort(voters);
			for (int i=0; i<voters.length; i++)
				System.out.println(voters[i]);

			// Get next case.
			numCand = stdin.nextInt();
			numVoters = stdin.nextInt();
		}
	}
}

class voter implements Comparable<voter> {

	private String name;
	private int initPos;
	private int candNum;

	public voter(String s, int pos, int support) {
		name = s;
		initPos = pos;
		candNum = support;
	}

	// How we compare two voters - first by candidate, then by rank on list.
	public int compareTo(voter other) {
		if (this.candNum != other.candNum) return this.candNum - other.candNum;
		return this.initPos - other.initPos;
	}

	public String toString() {
		return name;
	}
}