On the game show The Last Word, the host begins a round by showing the contestant a string
For example, for C
on the whiteboard, the contestant could make one of the following four sets of choices:
- put the
A
beforeC
to formAC
, then put theB
beforeAC
to formBAC
- put the
A
beforeC
to formAC
, then put theB
afterAC
to formACB
- put the
A
afterC
to formCA
, then put theB
beforeCA
to formBCA
- put the
A
afterC
to formCA
, then put theB
afterCA
to formCAB
The word is called the last word when the contestant finishes writing all of the letters from CAB
(which happens to be the same as the original word). For a game with MJA
.
You are the next contestant on this show, and the host has just showed you the string
Input Specification
The first line of the input gives the number of test cases,
Output Specification
For each test case, output one line containing Case #x: y
, where
Limits
Time limit: 20 seconds per test set.
Memory limit: 1 GB.
Small Dataset
Large Dataset
Sample Input
7
CAB
JAM
CODE
ABAAB
CABCBBABC
ABCABCABC
ZXCASDQWE
Sample Output
Case #1: CAB
Case #2: MJA
Case #3: OCDE
Case #4: BBAAA
Case #5: CCCABBBAB
Case #6: CCCBAABAB
Case #7: ZXCASDQWE
Comments