// Arup Guha
// 5/20/2016
// Solution to 2014 NCNA Problem E: Anagram Pyramids

import java.util.*;

public class pyramids {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int loop = 1;

                // Process all cases.
		while (stdin.hasNext()) {

                        // Store dictionary as set of words that have
                        // letters in alpha order.
			int n = stdin.nextInt();
			HashSet<String> dictionary = new HashSet<String>();
			for (int i=0; i<n; i++)
				dictionary.add(convert(stdin.next()));

                        // Run and output each query.
			System.out.println("Case "+loop+":");
			int items = stdin.nextInt();
			for (int i=0; i<items; i++) {
				String start = convert(stdin.next());
				String end = convert(stdin.next());
				boolean res = search(start, end, dictionary);
				if (res) System.out.println("yes");
				else	 System.out.println("no");
			}

                        // Go to the next case.
			loop++;
		}
	}

	public static boolean search(String s, String t, HashSet<String> dictionary) {

                // If we're the same length, we have to be the same set of letters.
		if (s.length() == t.length()) return s.equals(t);

                // Try inserting each letter into set s and see if the result is in the dictionary.
		for (char c='A'; c<='Z'; c++) {
			String tmp = insert(s, c);
			if (dictionary.contains(tmp)) {
				boolean res = search(tmp, t, dictionary);
				if (res) return true;
			}
		}

                // If we get here, no solution was found.
		return false;
	}

        // Assumes s in alpha order, puts c in its correct place.
	public static String insert(String s, char c) {
		int i = 0;
		while (i<s.length() && c > s.charAt(i)) i++;
		return s.substring(0,i) + c + s.substring(i);
	}

        // Sorts letters of s and returns this as a String.
	public static String convert(String s) {
		char[] arr = s.toUpperCase().toCharArray();
		Arrays.sort(arr);
		String res = new String(arr);
		return new String(arr);
	}
}
