{"problem":{"name":"[USACO23JAN] Moo Operations B","description":{"content":"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 \\le Q \\le 100)$, where the only characters are `","description_type":"Markdown"},"platform":"Luogu","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":{"LuoguStyle":"P2"},"is_remote":true,"is_sync":true,"sync_url":null,"sign":"LGP9012"},"statements":[{"statement_type":"Markdown","content":"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 \\le Q \\le 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:\n\n1. Replace either the first or last character with its opposite (so that 'M' becomes 'O' and 'O' becomes 'M').\n2. Delete either the first or last character. \n\nUnfortunately, 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. \n\n## Input\n\nThe first line of input contains the value of $Q$.\n\nThe 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. \n\n## Output\n\nOutput the answer for each input string on a separate line. \n\n[samples]\n\n## Note\n\n### Explanation for Sample 1\n\nA sequence of $4$ operations transforming the first string into `MOO` is as follows:\n\nReplace the last character with `O` (operation 1)  \nDelete the first character (operation 2)  \nDelete the first character (operation 2)  \nDelete the first character (operation 2)  \n\nThe second string cannot be transformed into `MOO`. The third string is already `MOO`, so no operations need to be performed. \n\n### Scoring\n\n - Inputs $2-4$: Every string has length at most $3$.\n - Inputs $5-11$: No additional constraints.","is_translate":false,"language":"English"}],"meta":{"iden":"LGP9012","tags":["贪心","USACO","2023","分类讨论"],"sample_group":[["3\nMOMMOM\nMMO\nMOO","4\n-1\n0"]],"created_at":"2026-03-03 11:09:25"}}