// Arup Guha
// 1/18/1014
// Solution to 2009 UCF Local Contest Problem: Wheel of Universally Copious Fortune

import java.util.*;

public class copious {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numWords = stdin.nextInt();
		String[] dictionary = new String[numWords];

		// Read in the dictionary.
		for (int i=0; i<numWords; i++)
			dictionary[i] = stdin.next();

		// Go through each query.
		int numQueries = stdin.nextInt();
		for (int loop=1; loop<=numQueries; loop++) {

			String mold = stdin.next();

			// Store each answer.
			ArrayList<String> ans = new ArrayList<String>();
			for (int i=0; i<dictionary.length; i++)
				if (match(mold, dictionary[i]))
					ans.add(dictionary[i]);

			// Print out solutions and number of them.
			System.out.println("Word #"+loop+": "+mold);
			for (int i=0; i<ans.size(); i++)
				System.out.println(ans.get(i));
			System.out.println("Total number of candidate words = "+ans.size());
			System.out.println();
		}
	}

	// Returns true iff mold matches word.
	public static boolean match(String mold, String word) {

		// Lengths have to match.
		if (mold.length() != word.length()) return false;

		// See if a stated character doesn't match that of the word...
		for (int i=0; i<mold.length(); i++)
			if (mold.charAt(i) != '-' && mold.charAt(i) != word.charAt(i))
				return false;
		return true;
	}
}