#include <iostream>
#include <queue>
#include <string>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

int main(){
    int n; cin>>n;
    while(n--){
        int out=0, p=0, b=0;
        while(out!=3){
            string s; cin>>s;
            if(s[0]=='O') out++;
            else if(s.back()=='T'){
                b++;
                if(b>=4) b--, p++;
            }
            else p+=b+1, b=0;
        }
        cout<<p<<endl;
    }
}