# memoization dictionary to store previous results memo = {} # recursive function with memoization def solve(i, current_string): if current_string in memo: return memo[current_string] if current_string == S: memo[current_string] = True return True if i >= n: return False # generate all possible combinations of strings in list A result = solve(i + 1, current_string + A[i]) or solve(i + 1, current_string) memo[current_string] = result return result T = int(input()) for t in range(1, T + 1): n = int(input()) # use list comprehension to create list A A = [input() for i in range(n)] m = int(input()) S = input() # reset memoization dictionary for each test case memo = {} # call recursive function to check if S can be formed by concatenating strings in A if solve(0, ""): print("Case #{}: YES".format(t)) else: print("Case #{}: NO".format(t))