import sys, math, itertools, functools, collections input = sys.stdin.readline def solve(testcase): a = input().strip().split() n = int(input()) s = set() for i in range(n): t = input().strip() cur = ''.join(a[ord(j)-65] for j in t) s.add(cur) print('Case #'+str(testcase)+':', 'YES' if len(s) < n else 'NO') for testcase in range(1, int(input())+1): solve(testcase)