// Arup Guha
// 1/20/2022
// Alternate Solution to CS2 Program #1: Politics (Uses a TreeMap from a unique long ID to supporter name)

import java.util.*;

public class politics_v3 {

	final public static int SHIFT = 20;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nCandidates = stdin.nextInt();
		int nSupporters = stdin.nextInt();
		
		while (nCandidates > 0) {
		
			// Just map each candidate to a unique ID, starting at 0.
			int id = 0;
			HashMap<String,Integer> map = new HashMap<String,Integer>();
			for (int i=0; i<nCandidates; i++) {
				String name = stdin.next();
				map.put(name, id++);
			}
			
			// All supporters will be stored here, mapping their unique int ID to their name,
			TreeMap<Long,String> supporters = new TreeMap<Long,String>();
				
			// Now go through supporters.
			for (int i=0; i<nSupporters; i++) {
			
				// Read in supporter and candidate.
				String sup = stdin.next();
				String cand = stdin.next();
				
				// Add if we have a new candidate.
				if (!map.containsKey(cand))
					map.put(cand, id++);
				
				// Get the candidate ID and store unique long for each supporter.
				long candID = map.get(cand);
				long supID = (candID<<SHIFT)+i;
				
				// Map the long identifier for this supporter to their name.
				supporters.put(supID, sup);
			}
			
			// Now go through the candidates in order.
			while (supporters.size() > 0)
				System.out.println(supporters.pollFirstEntry().getValue());
			
			// Read in next case.
			nCandidates = stdin.nextInt();
			nSupporters = stdin.nextInt();
		}
	}
}