def encode_word(word, mapping): # Encode a word using the given mapping encoded_word = "" for letter in word: encoded_word += str(mapping[letter]) return encoded_word def has_collisions(words, mapping): # Check if there are any collisions between the encoded words encoded_words = set() for word in words: encoded_word = encode_word(word, mapping) if encoded_word in encoded_words: return True else: encoded_words.add(encoded_word) return False # Read input and solve each test case T = int(input()) for i in range(1, T+1): mapping = input().split() mapping = {chr(ord('A')+j): int(mapping[j]) for j in range(26)} N = int(input()) words = [] for j in range(N): words.append(input().strip()) if has_collisions(words, mapping): print("Case #{}: YES".format(i)) else: print("Case #{}: NO".format(i))