Problem C. Magical bubbles

(Remark by Ho Leung:)
Today is Niels's birthday and hence he cannot join the contest today. Happy birthday to Niels anyway!

Problem

X is a great magician!! He is able to achieve any aspiration of someone else. Today is his neighbor Niels's birthday. X decides to give Niels a chance to realize an aspiration, but Niels must first solve a difficult problem for X, as follows.

X has a string of Magical Bubbles, where each bubble may be R(Red), G(Green) or B(Blue) in colour. The ordering of the bubbles are given initially. X can combine two adjacent bubbles into a new bubble, which will take up the original position of the two bubbles. Different arrangement of two original bubbles will make a direct impact on the colour of the new bubble. X hopes to convert this long string of bubbles into a single bubble with a designated colour, but he doesn't know whether there is a solution. Your task is to write a program to help Niels answer this problem.

Input

The input starts with a positive integer C indicating the number of test cases, where 1 ≤ C ≤ 20.

Each test case will begin with a number of lines, each containing 3 characters where a character will be either R, G or B only (representing Red, Green and Blue respectively). A line c1c2c3 specifies that X can combine a bubble of colour c1 on the left and a bubble of colour c2 on the right into a new bubble of colour c3. No two lines will have the same c1 and c2, and the ordering of c1 and c2 is important. For each pair of colours which has not been specified, they can be combined to form a new bubble with colour same as the right bubble. For example, a line RGB means that a red bubble on the left and a green bubble on the right can be combined into a blue bubble; if no line contains BGc3, then B and G can be combined into G. After these lines, there will be a line containing a string of N+1 characters, where 3 ≤ N ≤ 100 and the characters will R, G or B only. The first N characters specify the original arrangement of the bubbles, and the last character specifies the colour of final bubble that X wants.

Output

For each test case, output one line containing "Case x:" followed by either "Yes" or "No", indicating whether the string of bubbles can be combined into a new bubble with the designated colour. For example, if the input is
1
RGB
RRRGGBB
Consider RRRGGB -> RR(RG)GB -> RRBGB -> R(RB)(GB) -> RBB -> (RB)B -> BB -> B Thus the output is
Case 1: Yes

Sample Input

2
RGB
GBB
RRRGGBB
RGR
RBR
GGR
GBR
BGR
BBR
RGBRGBRG

Sample output

Case 1: Yes
Case 2: No

Remarks

Wish all of you an enjoyable contest!! :)

Author

Niels Tsang