Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	n, _ := strconv.Atoi(stdin.Text())
	for i := 0; i < n; i++ {
		base := [3]bool{}
		score, out := 0, 0
		for stdin.Scan() {
			switch stdin.Text() {
			case "HIT":
				if base[2] { // 3塁
					score++
				}
				if base[1] { // 2塁
					base[2] = true
					base[1] = false
				}
				if base[0] { // 1塁
					base[1] = true
				}
				base[0] = true
			case "HOMERUN":
				for i := 0; i < 3; i++ {
					if base[i] {
						score++
						base[i] = false
					}
				}
				score++
			case "OUT":
				out++
			}
			if out >= 3 {
				fmt.Println(score)
				break
			}
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define X real()
#define Y imag()
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using comd = complex<double>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

double CalcDist(comd p1, comd p2){
  return sqrt(pow(p1.X - p2.X,2.0) + pow(p1.Y -p2.Y,2.0));
}

template <typename T>
void out(deque < T > d)
{
  for(size_t i = 0; i < d.size(); i++)
  {
    debug(d[i]);
  }
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int score=0;
  string s;
  int n,out=0,runner = 0;
  cin >> n;
  rep(i,n){
    out = runner = 0,score=0;
    while(1){
      cin >> s;
      if(s == "HIT"){
        if(runner & 4){
          score++;
        }
        runner = runner<<1;
        runner &= 7;
        runner |= 1;
      }
      else if(s == "OUT"){
        ++out;
      }
      else{
        if((runner & 7) == 7){
          score += 4;
          runner = 0;
        }
        else if((runner & 7) == 3){
          score += 3;
          runner = 0;
        }
        else if((runner & 7) == 1){
          score += 2;
          runner = 0;
        }
        else{
          score++;
        }
      }
//       cout << runner << " "<< score << " " << out << endl;
      if(out == 3) break;
    }
    cout << score<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <string>

class Baseball{
    private:
        std::queue<int> runner;
        int out_count;
        int score;
        
    public:
        void initRunner(){
            while(runner.empty() != true){
                runner.pop();
            }
            for(int i = 0; i < 3; ++i){
                runner.push(0);
            }
        }
        
        Baseball(){
            out_count = 0;
            score = 0;
            initRunner();
        }
        
        ~Baseball(){}
        
        void hit(){
            if(runner.back() == 1) ++score;
            runner.pop();
            runner.push(1);
        }
        
        void homerun(){
            do{
                if(runner.back() == 1) ++score;
                runner.pop();
            } while(runner.empty() != true);
            ++score;
            initRunner();
        }
        
        int getScore(){
            initRunner();
            std::string str;
            while(out_count != 3){
                std::cin >> str;
                if(str == "HIT") hit();
                if(str == "HOMERUN") homerun();
                if(str == "OUT") ++out_count;
            }
            return score;
        }
};

int main(void){
    int N;
    std::cin >> N;
    for(int i = 0; i < N; ++i){
        Baseball baseball;
        std::cout << baseball.getScore() << std::endl;
    }
}

C++: #include<stdio.h>
#include<string.h>

int main(){

    int i,j,n;
    int out=0;
    char s[10];
    scanf("%d", &n);
    if(n!=0){
      int base[4]={};
      while(n>0){
	if(out<3){
	  scanf("%s", s);
	  if(strcmp(s, "HIT") ==0)
	    {base[3]+=base[2];base[2]=base[1];base[1]=base[0];base[0]=1;}
	  if(strcmp(s, "HOMERUN") ==0)
	    {base[3]=base[3]+base[2]+base[1]+base[0]+1;base[0]=0;base[1]=0;base[2]=0;}
	  if(strcmp(s, "OUT") ==0)
	    out++;
	  //printf("%d %d %d %d\n", base[0], base[1], base[2], base[3]);
	}
	else
	  {out=0;printf("%d\n", base[3]);n--;base[0]=0;base[1]=0;base[2]=0;base[3]=0;}
      }
    }
  return 0;
}

C++: #include <iostream>
#include <string>

class Baseball{
	private:
	int out;
	int runner;
	int point;

	public:
	void init();
	int get_out();
	int get_point();
	void event_hit();
	void event_homerun();
	void event_out();
};

void Baseball::init(){
	out = 0;
	runner = 0;
	point = 0;
}

int Baseball::get_out(){
	return out;	
}

int Baseball::get_point(){
	return point;
}

void Baseball::event_hit(){
	if(runner == 3){
		++point;
	}else{
		++runner;
	}
}

void Baseball::event_homerun(){
	point += ++runner;
}

void Baseball::event_out(){
	++out;
}

int main(void){
	int num;
	std::cin >> num;

	std::string event;
	Baseball base;
	base.init();
	for(int i = 0; i < num; ++i){
		while(base.get_out() < 3){
			std:: cin >> event;

			if(event == "OUT"){
				base.event_out();		
			}else if(event == "HIT"){
				base.event_hit();
			}else if(event == "HOMERUN"){
				base.event_homerun();
			}
		}
		std::cout << base.get_point() << std::endl;
		base.init();
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>

int main(void)
{
	int base=0,out=0,score=0,e,i,j;
	char str[10];
	for(;;){
		scanf("%d",&e);
		if(e==0)break;
		for(i=0;i<e;){
			scanf("%s",&str);
			if(!strcmp(str,"HIT"))base++;
			
			else if(!strcmp(str,"OUT")){
				out++;
			}
			
			else if(!strcmp(str,"HOMERUN")){
				score=score+base+1;
				base=0;
			}
			if(base>3){
				base--;
				score++;
			}
			if(out==3){
				out=0;
				printf("%d\n",score);
				i++;
				base=0;
				score=0;
			}
		}
	}
	return 0;
}