// Arup Guha
// 1/23/2015
// Solution to 2013 MCPC Problem H: Sort Me!

import java.util.*;

public class h {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int loop = 1;

        // Go through all cases.
		while (n != 0) {

			// Read in this case.
			word.setOrder(stdin.next());
			word[] list = new word[n];
			for (int i=0; i<n; i++)
				list[i] = new word(stdin.next());

			// Solve it!
			Arrays.sort(list);

			// Print output with case header.
			System.out.println("year "+loop);
			for (int i=0; i<n; i++)
				System.out.println(list[i]);

			// Get next case.
			n = stdin.nextInt();
			loop++;
		}
	}
}

class word implements Comparable<word> {

	public static HashMap<Character,Integer> order;
	public String item;

	public word(String w) {
		item = w;
	}

	// Sets up ordering of letters based on myOrder.
	public static void setOrder(String myOrder) {
		order = new HashMap<Character,Integer>();
		for (int i=0; i<myOrder.length(); i++)
			order.put(myOrder.charAt(i), i);
	}

	public int compareTo(word other) {

		// Go through both words, letter by letter.
		int i = 0;
		while (i < item.length() && i < other.item.length()) {

			// Get rank of these corresponding characters.
			int thisIndex = order.get(item.charAt(i));
			int otherIndex = order.get(other.item.charAt(i));

			// Break tie here.
			if (thisIndex != otherIndex) return thisIndex-otherIndex;

			// Go to next character.
			i++;
		}

		// If we get here, we break ties by lengths of strings.
		return item.length()-other.item.length();
	}

	public String toString() {
		return item;
	}
}
