// Arup Guha
// 4/15/2023
// Solution to 2023 Code Jam Farewell Round A Problem A: Colliding Encoding

import java.util.*;

public class a {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=1; loop<=nC; loop++) {
			
			// Read code.
			int[] code = new int[26];
			for (int i=0; i<26; i++)
				code[i] = stdin.nextInt();
				
			// Will store each word as a number, based on length.
			HashSet<Long>[] sets = new HashSet[11];
			for (int i=0; i<=10; i++) sets[i] = new HashSet<Long>();
			
			int n = stdin.nextInt();
			
			boolean collide = false;
			
			// Loop through words.
			for (int i=0; i<n; i++) {
			
				char[] s = stdin.next().toCharArray();
				
				// Calculate integer value.
				long x = 0;
				for (int j=0; j<s.length; j++)
					x = x*10 + code[s[j]-'A'];
					
				// Oops there is a collision.
				if (sets[s.length].contains(x)) collide = true;
				
				// Add to our set of words of the appropriate length.
				sets[s.length].add(x);
			}
			
			// Output accordingly.
			if (collide)
				System.out.println("Case #"+loop+": YES");
			else
				System.out.println("Case #"+loop+": NO");
		}
	}
}