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 <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
    os<<'[';
    rep(i,a.size()) os<<(i?" ":"")<<a[i];
    return os<<']';
}



int main() {
   int n; cin >> n; 
   int score = 0;
   int runner = 0;
   int out = 0;
   for (; n != 0; ) {
       string s; cin >> s;
       if (s == "HIT") runner++;
       if (runner == 4) {
           score++;
           runner--;
       }
       if (s == "HOMERUN") {
           score += runner + 1;
           runner = 0;
       }
       if (s == "OUT") out++;
       if (out == 3) {
           runner = 0;
           cout << score << endl;
           score = 0;
           out = 0;
           n--;
       }
   }

    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main()
{
	int out = 0;
	int r1 = 0;
	int r2 = 0;
	int r3 = 0;
	int t = 0;
	string k;
	int a = 0;
	cin >> a;
	for (int i = 1; i <= a; i++)
	{
		out = 0;
		t = 0;
		r1 = 0;
		r2 = 0;
		r3 = 0;
		while (out != 3)
		{
			cin >> k;
			if (k == "OUT")
			{
				out++;
			}
			if (k == "HIT")
			{
				if (r3 == 1)
				{
					t++;
					r3 = 0;
				}
				if (r2 == 1)
				{
					r3 = 1;
					r2 = 0;
				}
				if (r1 == 1)
				{
					r2 = 1;
					r1 = 0;
				}
				r1 = 1;
			}
			if (k == "HOMERUN")
			{
				if (r3 == 1)
				{
					t++;
					r3 = 0;
				}
				if (r2 == 1)
				{
					t++;
					r2 = 0;
				}
				if (r1 == 1)
				{
					t++;
					r1 = 0;
				}
				t++;

			}
		}
		cout << t << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(){
    int n;
    cin >> n;

    for(int i=1; i<=n; i++){
        vector<bool> base;
        for(int j=1; j<=3; j++){
            base.push_back(false);
        }
        int out = 0;
        int point = 0;
        while(1){
            if(out == 3) break;
            string str;
            cin >> str;
            if(str == "HIT"){
                if(base[2] == true){
                    base[2] = false;
                    point++;
                }
                if(base[1] == true){
                    base[2] = true;
                    base[1] = false;
                }
                if(base[0] == true){
                    base[1] = true;
                }
                base[0] = true;
            }
            if(str == "HOMERUN"){
                int cnt = 0;
                for(int j=0; j<3; j++){
                    if(base[j] == true){
                        base[j] = false;
                        cnt++;
                    }
                }
                point += cnt + 1;
            }
            if(str == "OUT"){
                out++;
            }
        }
        cout << point << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
	int n,i=0,out=0,g=0;
	int cange=0,score[2]={0,0};
	char evn[100][7];
	int base[4]={0,0,0,0};
	cin >> n;
	while(n!=g){
		cin >> evn[i];
		if(evn[i][1]=='I'){
			base[3]+=base[2];
			base[2]=base[1];
			base[1]=base[0];
			base[0]=1;
		}
		if(evn[i][1]=='O'){
			base[3]+=base[2]+base[1]+base[0]+1;
			base[2]=0;base[1]=0;base[0]=0;
		}
		if(evn[i][0]=='O'){
			out++;
		}
		if(out==3){
			out=0;
			base[0]=0;
			base[1]=0;
			base[2]=0;
			if(cange==0){
			score[0]=base[3];
			base[3]=0;
			cange=1;
			g++;
			}
			else{
				score[1]=base[3];
				score[3]=0;
				cange=0;
				g++;
			}
		}
	}
	cout << score[0] << endl <<score[1] << endl;
return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(void)
{
	string instr;
	int N;

	cin >> N;

	while(N--, N >= 0){
		int out = 0;
		int pos[4] = {};

		while(out < 3){
			cin >> instr;

			if(instr == "HIT"){
				if(pos[2]){
					pos[3] += pos[2];
					pos[2] = 0;
				}
				swap(pos[2],pos[1]);
				swap(pos[1],pos[0]);
				pos[0] = 1;
			}
			else if(instr == "HOMERUN"){
				pos[3] += pos[0] + pos[1] + pos[2] + 1;
				pos[0] = pos[1] = pos[2] = 0;
			}
			else if(instr == "OUT")
				out++;
	//		cout << pos[0] << ' ' << pos[1] << ' ' << pos[2] << ' ' << pos[3] << endl;
		}
		cout << pos[3] << endl;
	}
			

	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;

int main(){
	int inning_num;
	
	cin >> inning_num;
	for (int i = 0; i < inning_num; i++){
		int out = 0;
		unsigned char state = 0;
		int score = 0;
		while (out != 3){
			string in;
			cin >> in;
			if (in == "OUT"){
				out++;
			}
			else if (in == "HIT"){
				state <<= 1;
				state |= 0x0001;
			}
			else if (in == "HOMERUN"){
				state <<= 1;
				state |= 0x0001;
				state <<= 3;
			}
			for (unsigned char j = (1<<3); j; j <<= 1){
				if (state & j) {
					score++;
					state &= ~j;
				}
			}
		}
		cout << score << "\n";
	}
}

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


class Baseball {
public:
    int score;                //得点
    int count;                //アウトカウント
    std::queue<int> runner;   //ランナー

    //コンストラクタ
    Baseball(int sc,int co){
        score = sc;
        count = co;
        while(!runner.empty()){
            runner.pop();
        }
    }
    
    //ヒット
    void hit(){
        if(runner.size() == 3){
            ++score;
        }
    }
    
    //ホームラン
    void homerun(){
        while(!runner.empty()){
            ++score;
            runner.pop();
        }
        ++score;
    }
    
    //アウト
    void out(){
        ++count;
    }
};


int main(){
    int n;
    std::string event;
    
    std::cin >> n;
    
    Baseball game(0,0);
    
    while(n > 0 && std::cin >> event){
        if(event == "HIT")
            game.hit();
        else if(event == "HOMERUN")
            game.homerun();
        else
            game.out();
        
        if(game.count == 3){
            --n;
            std::cout << game.score << std::endl;
            game = Baseball(0,0);
        }
    }
}