#include <stdio.h>
int main() {char T[20];int N,pnt,ot,base;while (scanf("%d",&N)!=EOF) {while (N--) {pnt=ot=base=0;while (1) {scanf("%s",&T);if (T[0]=='O') ot++; else if (T[1]=='O') pnt+=base+1,base=0; else base++,pnt+=((base&4)?1:0),base-=((base==4)?1:0);if (ot==3) printf("%d\n",pnt),pnt=ot=base=0;}}} return 0;}