// Arup Guha
// 8/26/2016
// Alternate solution for 2016 UCF Locals Problem: Phoneme Palindromes

import java.util.*;
import java.io.*;

public class palind_arup {

	public static void main(String[] args) throws Exception {

		Scanner stdin = new Scanner(System.in);

		// Get # of cases
		int n = stdin.nextInt();

		// Try each case.
		for (int loop=1; loop<=n; loop++) {

            // Useful to keep track of substitutions.
			HashMap<Character,Character> sub = new HashMap<Character,Character>();

			int numPairs = stdin.nextInt();

			// Get all pairs of substitutions and store in the map, just one way.
			for (int i=0; i<numPairs; i++) {
				char c1 = stdin.next().charAt(0);
				char c2 = stdin.next().charAt(0);
				sub.put(c1, c2);
			}

			// Case header.
			System.out.println("Test case #"+loop+":");

			int numWords = stdin.nextInt();

			// Process each word.
			for (int wC=0; wC<numWords; wC++) {

				String word = stdin.next();
				int len = word.length();

				// Form the string to check - just using one way substitution.
				String check = "";
				for (int i=0; i<len; i++) {
					if (sub.containsKey(word.charAt(i)))
						check = check + sub.get(word.charAt(i));
					else
						check = check + word.charAt(i);
				}

				// Echo input.
				System.out.print(word+" ");

				// Now check to see if it's a pal.
				boolean res = true;
				for (int i=0; i<len/2; i++)
					if (check.charAt(i) != check.charAt(len-1-i))
						res = false;
				if (res)
					System.out.println("YES");
				else
					System.out.println("NO");

			}

			// Between cases.
			System.out.println();
		}
	}
}
