#include<stdio.h>
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		char c[10];
		int sum=0,people=0,out=0;
		while(out<3){
			scanf("%s",c);
			if(c[1]=='I'){
				people++;
				if(people==4){
					sum+=1;
					people=3;}}
			if(c[1]=='O'){
				sum+=people+1;
				people=0;}
			if(c[1]=='U'){
				out++;}}
		printf("%d\n",sum);}
	return 0;}
