PROBLEM 1: Musical Themes


Description:

A Theme is a sequence of notes that is repeated, perhaps multiple times, in music. Sometimes these repetitions may be transposed (adjusted higher or lower but with the same intervals). Your task is to determine the length of the longest theme in a piece of music. In order to simplify this problem we will ignore the existence of sharps and flats. Notes range from A through G which are repeated for higher and lower octaves (eg. a theme consisting of notes of incrementally higher pitches might be DEFGABC if the A is in the next higher octave.)

Input:

The first line contains an integer that indicates the number of tunes. Each following line contains one tune. A tune is simply a sequence of at most 50 notes (upper case letters from A through G) with no delimiting characters. There is no indication of what octaves the notes are in. When in doubt, be liberal - always assume the octave that extends the repetition to be the longest possible.

Example Input:

2 
ABCDFCCGGDFCCAGAGFAEE 
ABDGB

Output:

For each tune give the note sequence for the longest theme. If there is more than one longest theme, choose the leftmost one. In the first example above, the longest theme is length 4: DFCC. It is both simply repeated once and is transposed up to FAEE.

Example Output:

DFCC 
BD