import java.io.*;
import java.util.Scanner;

public class BetterAlien {
	/**
	 * Application entry point. 
	 * Parses command line and open input/output files
	 * then runs application.
	 * The first argument is the name of the input file,
	 * The second argument is the name of the output file,
	 * If output is not given uses standard output (console)
	 * If also input is not given, uses standard input (keyboard)
	 * @param args are command line arguments
	 * @throws IOException if some error occurs while reading or writing files
	 */
    public static void main(String[] args) throws IOException {
    	Reader reader = null;
    	Writer writer = null;
    	
    	if (args.length >= 1) {
    		String inputFilename = args[0];
    		try {
    			reader = new FileReader(inputFilename);
    		} catch (FileNotFoundException e) {
    			System.err.println("Cannot read file: " + inputFilename);
    			System.exit(1); // terminate with error code
    		}
    	} else {
    		// no input filename specified: use standard input
    		reader = new InputStreamReader(System.in);
    	}

		if (args.length >= 2) {
			String outputFilename = args[1];
			try {
				writer = new FileWriter(outputFilename);
			} catch (FileNotFoundException e) {
				System.err.println("Cannot write file: " + outputFilename);
				System.exit(1); // terminate with error code
			}
		} else {
			// no output filename specified: use standard output
			writer = new OutputStreamWriter(System.out);
		}
		
		if (args.length >= 3) {
			System.err.println("Too many arguments");
			System.exit(1); // terminate with error code
		}
		
		// start code
		BetterAlien alien = new BetterAlien();
		alien.run(reader,writer);
		reader.close();
		writer.close();
    }

    Scanner input;
    Writer output;

    int L;
    int D;
    int N;
    
    String[] dictionary;
    
    /**
     * actually run application
     */
    public void run(Reader reader, Writer writer) throws IOException {    	
    	input = new Scanner(reader);
    	output = writer;
    	
    	L = input.nextInt();
    	D = input.nextInt();
    	N = input.nextInt();
	
		dictionary = new String[D];		
		for (int i=0; i<D; i++) {
		    dictionary[i] = input.next();
		}
	
		for (int i=0; i<N; i++) {
		    String pattern;
		    pattern = input.next();
		    int K = countMatches(pattern);
		    output.write("Case #" + (i+1) + ": " + K + "\n");
		}
    }

    /**
     * Counts the number of matches of given pattern against dictionary words
     * @param pattern is the pattern to be matched
     * @return the number of dictionary words matching pattern
     */
    int countMatches(String pattern) {
		int count = 0;
		for (int i=0; i<D ; ++i) {
		    if (matches(pattern, dictionary[i])) {
			count ++;
		    }
		}
		return count;
    }
    
    /**
     * Checks whether the given word matches the given pattern
     * @param pattern is the pattern
     * @param word is the dictionary word
     * @return true if the word matches the pattern
     */
    Boolean matches(String pattern, String word) {
    	int j=0; // index in pattern
    	int i;   // index in word
    	assert(word.length() == L);
    	for (i=0; i<L; i++) {
    		assert(j < pattern.length());
    		if (pattern.charAt(j) == word.charAt(i)) {
    			// the characters match literally
    			j++; // next pattern character
    		} else if (pattern.charAt(j) == '(') {
    			j++; // skip open parenthesis
    			Boolean found = false;
    			for (; pattern.charAt(j)!=')'; j++) {
    				if (pattern.charAt(j) == word.charAt(i))
    					found = true;
    			}
    			if (!found) {
    				return false;
    			}
    			assert(pattern.charAt(j) == ')');
    			j++; // skip closed parenthesis
    		} else {
    			// character at pattern is different from character at word
    			return false;
    		}
    	}
    	return true;
    }
}
