PLAY in BASIC

Given a Music Macro Language score, find the character length of the shortest score with identical pitches, note lengths, volumes, and rests.

Medium7Dynamic programmingSimulationStringNo attempts yetTime limit5sMemory limit512 MB

Problem

An old BASIC dialect has a PLAY statement. It takes a score written in Music Macro Language (MML) as a string and plays it. This problem uses the MML commands below.

Note: Cn, C+n, C-n, Dn, D+n, D-n, En, ... (n is one of 1, 2, 4, 8, 16, 32, 64, 128, plus dots)

A note name is one of the seven letters C, D, E, F, G, A, B, and it can be followed by + for a sharp or - for a flat. The letters C through B make up one octave, laid out as in the figure below. Each note belongs to the current octave, which the octave commands set. The note C- of the lowest octave (1) and the note B+ of the highest octave (8) cannot be played.

A duration specifier is one of 1, 2, 4, 8, 16, 32, 64, 128, where 1 is a whole note, 2 a half note, 4 a quarter note, 8 an eighth note, and so on. The specifier can be omitted, and then the default duration set by the L command is used. Dots can follow the number. One dot adds half of the preceding duration, so 4. is 4 plus 8, which is 1.5 times as long as 4. A note can carry several dots, and each extra dot adds half of what the previous dot added. 4.. is 4 plus 8 plus 16, and 4... is that plus 32. The length a dot adds can never be shorter than 128, so 128. and 32... are not accepted. Dots written without a number extend the default duration, so C. is the same as C4. when the default duration is 4. C4C8 and C4. are different: the first is two separate notes, the second is one note.

Rest: Rn (n is one of 1, 2, 4, 8, 16, 32, 64, 128, plus dots)

The R command rests for the given duration. The duration is written the same way as for a note and can be omitted too. R4R8 and R4. are the same, because both rest for 4 plus 8. That differs from how C4C8 and C4. relate.

Octave: On (n is 1 to 8), <, >

The O command sets the current octave to the given number. > raises it by one and < lowers it by one. These commands can never take the octave below 1 or above 8. The current octave starts at 4.

Default duration: Ln (n is one of 1, 2, 4, 8, 16, 32, 64, 128)

The L command sets the default duration. The number is written the same way as for a note, but it cannot be omitted and cannot be followed by dots. The default duration starts at 4.

Volume: Vn (n is 1 to 255)

The V command sets the current volume. A larger value is louder. The volume starts at 100.

Two MML sequences play the same music when all of the following hold. They contain the same number of notes. Paired in order, every note has the same pitch, the same length and the same volume. The silence between each pair of neighbouring notes has the same length. Only the pitch of the sound matters, so B+ in octave 4 and C in octave 5 are the same note.

You are given an MML sequence. Find the number of characters in the shortest MML sequence that plays the same music. The octave, the volume and the default duration left behind at the end of the shortest sequence do not have to match those of the original.

Input

The input has several data sets. Each data set is one line holding an MML sequence of at most 100,000 characters. Only the commands described above appear. Every sequence has at least one note, and there is no rest before the first note or after the last note.

The end of the input is a line holding only *. That line is not a data set, so do not process it. There are at most 100 data sets, and the lengths of all sequences add up to at most 200,000 characters.

Output

For each data set print one line in the form Case k: n, where k is the number of the data set starting from 1 and n is the number of characters in the shortest MML sequence that plays the same music.