#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int q;
char str[N];
int main () {
freopen("moocaozuo.in", "r", stdin);
freopen("moocaozuo.out", "w", stdout);
cin >> q;
while (q--) {
cin >> (str+1); int lth = strlen(str+1), s = 3;
for (int i = 1;i <= lth-2;i++) {
int c = 0;
if (str[i+1] != 'O') continue;
if (str[i] != 'M') c++;
if (str[i+2] != 'O') c++;
s = min(s, c);
}
if (s >= 3) cout << -1 << endl;
else cout << s+lth-3 << endl;
}
return 0;
}