#!/usr/bin/env python3 def solve(encoding, n, s): res = [] for word in s: enc = "" for c in word: idx = ord(c) - ord('A') enc += encoding[idx] res.append(enc) if len(res) > len(set(res)): return True else: return False def main(): t = int(input()) for tc in range(1, t+1): encoding = input().split() n = int(input()) s = [] for _ in range(n): s.append(input()) res = "YES" if solve(encoding, n, s) else "NO" print(f"Case #{tc}: {res}") if __name__ == "__main__": main()