alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' t = int(input()) for u in range(t): nos = [int(i) for i in input().split()] code = {} for i in range(26): code[alphabet[i]] = str(nos[i]) n = int(input()) encoded = [] for _ in range(n): word = input() encoded.append(''.join(code[c] for c in word)) ans = 'NO' if len(set(encoded)) == len(encoded) else 'YES' print('Case #' + str(u+1) + ':', ans)