Skip to content

Moo Operations

Unrated
English

Language

Contribute a translation
Time limit
2000 ms
Memory limit
256 MB
Submissions
0
Correct
0
Solved by
0
AC rate

Statement

Because Bessie is bored of playing with her usual text string where the only characters are 'C,' 'O,' and 'W,' Farmer John gave her $Q$ new strings ($1 \leq Q \leq 100$), where the only characters are 'M' and 'O.' Bessie's favorite word out of the characters 'M' and 'O' is obviously "MOO," so she wants to turn each of the $Q$ strings into "MOO" using the following operations:

  1. Replace either the first or last character with its opposite (so that 'M' becomes 'O' and 'O' becomes 'M').
  2. Delete either the first or last character.

Unfortunately, Bessie is lazy and does not want to perform more operations than absolutely necessary. For each string, please help her determine the minimum number of operations necessary to form "MOO" or output $-1$ if this is impossible.

Input

The first line of input contains the value of $Q$.

The next $Q$ lines of input each consist of a string, each of its characters either 'M' or 'O'. Each string has at least 1 and at most 100 characters.

Scoring

  • Inputs 2-4: Every string has length at most $3$.
  • Inputs 5-11: No additional constraints.

Output

Output the answer for each input string on a separate line.

Examples

Sample input 1
3
MOMMOM
MMO
MOO
Sample output 1
4
-1
0

Notes

A sequence of $4$ operations transforming the first string into "MOO" is as follows:

The second string cannot be transformed into "MOO." The third string is already "MOO," so no operations need to be performed.

Tags

No tags yet

Source