#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int MOD=1000000007;
const int INF= int(1e9);
int main()
{
ios_base::sync_with_stdio(false);
int testCases;
cin>>testCases;
while(testCases--) {
string s;
cin>>s;
ll res=0;
int n=s.size();
vi before(n),after(n);
if(s[0]=='L') {
before[0]=1;
}
for(int i=1;i<n;i++) {
before[i]=before[i-1]+((s[i]=='L')?1:0);
}
if(s[n-1]=='L'){
after[n-1]=1;
}
for(int i=n-2;i>=0;i--){
after[i]=after[i+1]+((s[i]=='L')?1:0);
}
for(int i=0;i<n;i++) {
if(s[i]=='O') {
res+=(before[i]*after[i]);
}
}
cout<<res<<"\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCxpbnQ+IGlpOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IHZpOwp0eXBlZGVmIHZlY3RvcjxpaT4gdmlpOwoKY29uc3QgaW50ICBNT0Q9MTAwMDAwMDAwNzsKY29uc3QgaW50ICBJTkY9IGludCgxZTkpOwoKaW50IG1haW4oKQp7Cglpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGludCB0ZXN0Q2FzZXM7CiAgICBjaW4+PnRlc3RDYXNlczsKICAgIHdoaWxlKHRlc3RDYXNlcy0tKSB7CiAgICAgICAgc3RyaW5nIHM7CiAgICAgICAgY2luPj5zOwogICAgICAgIGxsIHJlcz0wOwogICAgICAgIGludCBuPXMuc2l6ZSgpOwogICAgICAgIHZpIGJlZm9yZShuKSxhZnRlcihuKTsKICAgICAgICBpZihzWzBdPT0nTCcpIHsKICAgICAgICAgICAgYmVmb3JlWzBdPTE7CiAgICAgICAgfQogICAgICAgIGZvcihpbnQgaT0xO2k8bjtpKyspIHsKICAgICAgICAgICAgYmVmb3JlW2ldPWJlZm9yZVtpLTFdKygoc1tpXT09J0wnKT8xOjApOwogICAgICAgIH0KICAgICAgICBpZihzW24tMV09PSdMJyl7CiAgICAgICAgICAgIGFmdGVyW24tMV09MTsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpPW4tMjtpPj0wO2ktLSl7CiAgICAgICAgICAgIGFmdGVyW2ldPWFmdGVyW2krMV0rKChzW2ldPT0nTCcpPzE6MCk7CiAgICAgICAgfQogICAgICAgIGZvcihpbnQgaT0wO2k8bjtpKyspIHsKICAgICAgICAgICAgaWYoc1tpXT09J08nKSB7CiAgICAgICAgICAgICAgICByZXMrPShiZWZvcmVbaV0qYWZ0ZXJbaV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBjb3V0PDxyZXM8PCJcbiI7CiAgICB9CgoJcmV0dXJuIDA7Cgp9Cg==