// Arup Guha
// 1/20/2022
// Alternate Solution to CS2 Program #1: Politics (uses an array of lists)

import java.util.*;

public class politics_v2 {

	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++);
			}
			
			// Max # of candidates.
			int maxSize = nCandidates+nSupporters;
			
			// Make a list of supporters for each candidate.
			ArrayList<String>[] supporters = new ArrayList[maxSize];
			for (int i=0; i<maxSize; i++) 
				supporters[i] = new ArrayList<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++);
					
				// Add this supporter to the right list.
				supporters[map.get(cand)].add(sup);
			}
			
			// Now go through the candidates in order.
			for (int i=0; i<maxSize; i++) {
				for (String s: supporters[i])
					System.out.println(s);
			}
			
			// Read in next case.
			nCandidates = stdin.nextInt();
			nSupporters = stdin.nextInt();
		}
	}
}