import sys input = sys.stdin.readline T=int(input()) for tests in range(T): D=list(map(int,input().split())) N=int(input()) S=[input().strip() for i in range(N)] X=[] for s in S: y=[] for c in s: y.append(D[ord(c)-65]) X.append(tuple(y)) #print(X) if len(X)==len(set(X)): print("Case #"+str(tests+1)+": "+"NO") else: print("Case #"+str(tests+1)+": "+"YES")