//////#include <stdio.h>
//////int main(){
//////	int x[102][102];
//////	int y[102][102];
//////	int i,j,k,h,w;
//////	int around=0;
//////	scanf("%d%d",&w,&h);
//////	for(i=1;i<=h;i++){
//////		for(j=1;j<=w;j++){
//////			scanf("%d",&x[j][i]);
//////		}
//////	}
//////	for(i=0;i<=w+1;i++){
//////		x[i][0]=0;
//////		x[i][h+1]=0;
//////	} 
//////	for(i=0;i<=h+1;i++){
//////		x[0][i]=0;
//////		x[w+1][i]=0;
//////	}
//////	for(i=0;i<=w+1;i++){
//////		for(j=0;j<=h+1;j++){
//////			y[i][j]=1;
//////		}
//////	}
//////	y[0][0]=0;
//////	for(k=0;k<10000;k++){
//////	for(i=0;i<=w+1;i++){
//////		for(j=0;j<=h+1;j++){
//////			if(j%2==0){
//////				if(x[i][j]==0){
//////					if(y[i][j]==0){ 
//////						y[i][j]=0;
//////					}
//////					if(y[i-1][j-1] == 0){
//////						y[i][j]=0;
//////					}
//////					if(y[i][j-1]==0){
//////						y[i][j]=0;
//////					}
//////					if( y[i+1][j]==0){
//////                    	y[i][j]=0;
//////					}
//////					if(y[i][j+1]==0){
//////                     	y[i][j]=0;
//////					}
//////					if(y[i-1][j+1]==0){
//////							y[i][j]=0;
//////					}
//////					if( y[i-1][j]==0){
//////						y[i][j]=0;
//////					}
//////				}
//////			}
//////			else{
//////				if(x[i][j]==0){
//////	           if(y[i][j]==0){
//////				y[i][j]=0;
//////			   }
//////			   if(y[i][j-1] == 0){
//////				   y[i][j]=0;
//////			   }
//////			   if( y[i+1][j-1]==0){
//////				   y[i][j]=0;
//////			   }
//////			   if( y[i+1][j]==0){
//////				   y[i][j]=0;
//////			   }
//////			   if( y[i+1][j+1]==0 ){
//////				   y[i][j]=0;
//////			   }
//////			   if( y[i][j+1]==0){
//////				   y[i][j]=0;
//////			   }
//////			   if( y[i-1][j]==0){
//////				   y[i][j]=0;
//////			   }
//////					}
//////				}
//////			}
//////	}
//////	}
//////
//////	for(i=0;i<=w+2;i++){
//////		for(j=0;j<=h+2;j++){
//////			if(y[i][j]==0){
//////				if(j%2==0){
//////					if(y[i-1][j-1]==1){
//////						around++;
//////					}
//////					if(y[i][j-1]==1){
//////						around++;
//////					}
//////					if(y[i+1][j]==1){
//////						around++;
//////					}
//////					if(y[i][j+1]==1){
//////						around++;
//////					}
//////					if(y[i-1][j+1]==1){
//////						around++;
//////					}
//////					if(y[i-1][j]==1){
//////						around++;
//////					}
//////
//////				}
//////				else{
//////					if(y[i][j-1]==1){
//////						around++;
//////					}
//////					if(y[i+1][j-1]==1){
//////						around++;
//////					}
//////					if(y[i+1][j]==1){
//////						around++;
//////					}
//////					if(y[i+1][j+1]==1){
//////						around++;
//////					}
//////					if(y[i][j+1]==1){
//////						around++;
//////					}
//////					if(y[i-1][j]==1){
//////						around++;
//////					}
//////				}
//////			}
//////		}
//////	}
//////	printf("%d\n",around);
//////	
//////	return 0;
//////}
//////#include <stdio.h>
//////#include <math.h>
//////  int main(){
//////long long n,i,j,a=0,num=0;
//////double c;
//////scanf("%I64d",&n);
//////for(i=0;i<n;i++){
//////	scanf("%lf",&c);
//////	c=sqrt(c);
//////	 int d=c;
////// for(j=2;j*j<=d;j++){
//////  if(d%j==0) {num=1; goto qq;}
////// }qq: 
//////if(num==0){
//////	if(c-d==0){
//////		if(d!=1 && d!=0){
//////	printf("YES\n"); 
//////	goto q;}
//////}
//////}
//////printf("NO\n");
//////q:
//////num=0;
//////	  }
//////	  return 0;
//////  }
////  #include <stdio.h>
////#include <math.h>
////int x[101][10001]={},n,m,i,j,k,min=2000000000,y[101]={},ss=0;
////char z[101][10001];
////  int main(){
////	  scanf("%d %d",&n,&m);
////	  for(i=1;i<=n;i++){
////		  scanf("%s",&z[i]);
////	  for(j=0;j<m;j++){
////		 x[i][j+1]=z[i][j]-'0';
////		 if(x[i][j+1]==1) y[i]++;
////	  }if(y[i]==0){printf("-1\n"); goto aaa;}
////	  }
////		  for(j=1;j<=m;j++){
////			  int vvv=m;
////			  for(i=1;i<=n;i++){
////			  if(x[i][j]==0){
////				  for(k=1;k<=m;k++){
////					  if(x[i][k]==1){
////						  double s=k-j;
////						  if(fabs(s)<=m/2){
////							  if(vvv>fabs(s)){
////								  vvv=fabs(s);}}
////						  else{ if(vvv>fabs(s)){
////							  vvv=m-fabs(s);}}
////						  }if(vvv==1) goto yy; }
////				  yy: ss+=vvv;
////				 if(ss>=min){continue;}
////			  }vvv=m;
////			  }
////			  if(min>ss){ min=ss;}
////			  ss=0;
////		  }
////		  printf("%d\n",min);
////aaa:
////return 0;
////  }
//#include <stdio.h>
//  int main(){
//int j,i,n,num=0;
//while(scanf("%d",&n)!=EOF){
//for(i=n-1;;i--){
//for(j=2;j*j<=i;j++){
//	if(j!=i){if(i%j==0){num=1;}}}
//if(num==0) {printf("%d ",i); goto qq;}
//num=0;}qq:
//for(i=n+1;;i++){
//for(j=2;j*j<=i;j++){
//	if(j!=i){if(i%j==0){num=1;}}}
//if(num==0) {printf("%d\n",i); goto rr;}
//num=0;} rr:; }
//return 0;
//}
#include <stdio.h>
#include <string.h>
  int main(){
  int a[3]={},c=0,n,i,p=0;
  char x[10];
   scanf("%d",&n);
   for(i=0;i<n;i++){
      while(c<3){
      scanf("%s",&x);
      if(x[0]=='H' && x[1]=='I'){
       int q=a[0];
       int r=a[1];
       if(a[2]==1){p++;}
       a[1]=q;
       a[2]=r;
       a[0]=1;
}
if(x[1]=='O'){
if(a[0]==1){
p++;
}
if(a[1]==1){
p++;
}
if(a[2]==1){
p++;
}
p++;
a[0]=0;
a[1]=0;
a[2]=0;
}
if(x[0]=='O'){
c++;
}}
printf("%d\n",p);
p=0;c=0;a[0]=0;a[1]=0;a[2]=0;
}
return 0;
}

Measured memory_bytes: 596