// Arup Guha
// 10/13/2015
// Solution to 2005 UCF HS Contst Problem: Array Revolutions

import java.util.*;

public class array {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Process each case
		while (n != 0) {

			// Get hash code for Martin.
			String code = stdin.next();

			// Process each item,
			for (int i=0; i<n; i++) {

				// Get this program and hash code.
				String name = stdin.next();
				String thisCode = stdin.next();

				// Only print if the code has been overwritten.
				if (thisCode.equals(code))
					System.out.println(name);
			}

			// Between cases...
			System.out.println();

			// Get next case.
			n = stdin.nextInt();
		}
	}
}