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 <iostream>
#include <string>

class Score {
public:
	Score() :out(0), runner(0), score(0), change(false) {};
	void batter(const int &event) {
		if (event == 0) {
			++runner;
		}
		else if (event == 1) {
			score += runner + 1;
			runner = 0;
		}
		else {
			if (++out == 3) {
				if (runner > 3) {
					score += runner - 3;
				}
				std::cout << score << std::endl;
				change = true;
			}
		}
	}
	const bool in_turn() const { return !change; }
private:
	int out, runner, score;
	bool change;
};

int main() {
	int n;
	std::cin >> n;
	for (auto i = 0; i < n; ++i) {
		Score score;
		while (score.in_turn()) {
			std::string event;
			std::cin >> event;
			if (event == "HIT") {
				score.batter(0);
			}
			else if (event == "HOMERUN") {
				score.batter(1);
			}
			else {
				score.batter(2);
			}
		}
	}
}

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

class Baseball{
    private:
        int score;
        int out;
        int runner;
        int inning;

    public:
        Baseball(); //コンストラクタ
        int getScore() { return score; }
        int addScore(int score) { score += score; }
        int getInning() { return inning; }
        bool addOut(); //アウトを加える，チェンジならtrue
        void moveOnRunner(); //ヒット時にランナーを進塁させる
        void slamHomeRun(); //ホームランを打ったとき
        void changeInning(); //イニングを変える，スコア, ランナーも0に戻す
};
Baseball::Baseball(){
    score = 0;
    out = 0;
    runner = 0;
    inning = 1;
}

bool Baseball::addOut(){
    out = (out + 1) % 3;
    return out == 0 ? true : false;
}

void Baseball::moveOnRunner(){
    if(runner < 3){
        runner++;
    } else if(runner == 3){
        runner = 3;
        score += 1;
    }
}

void Baseball::slamHomeRun(){
    score += (runner + 1);
    runner = 0;
}

void Baseball::changeInning(){
    score = 0;
    runner = 0;
    inning += 1;
}

int main(){
    Baseball b;
    int totalInning = 0;
    bool change_flag = false;
    std :: string event;
    std :: map<std :: string, int> map;

    map["HIT"] = 1;
    map["HOMERUN"] = 2;
    map["OUT"] = 3;

    std :: cin >> totalInning;
    std :: getline(std :: cin, event);
    while(b.getInning() <= totalInning){
        std :: getline(std :: cin, event);
        switch(map[event]){
            case 1:
                b.moveOnRunner();
                break;
            case 2:
                b.slamHomeRun();
                break;
            case 3:
                change_flag = b.addOut();
                break;
            default:
                break;
        }
        if(change_flag) {
            std :: cout << b.getScore() << std :: endl;
            b.changeInning();
            change_flag = false;
        }
    }
}

C++: #include <bits/stdc++.h>
#define INF 114514810
#define FOR(i, r, n) for(int i=(r); i<(n); i++) 
#define REP(i, n) FOR(i, (0), n)

using namespace std;

int n, point, out;
bool b[5];
string s;

int main(){
	cin >> n;
	REP(k, n){
		while (true){
			cin >> s;
			if (s == "HIT"){
				if (b[3]) { point++; b[3] = 0; }
				if (b[2]) { b[3] = 1; b[2] = 0; }
				if (b[1]) { b[2] = 1; }
				b[1] = 1;
			}
			else if (s == "OUT") {
				out++;
				if (out == 3) { cout << point << endl; break; }
			}
			else if (s == "HOMERUN"){
				if (b[1]) point++;
				if (b[2]) point++;
				if (b[3]) point++;
				point++;
				b[1] = b[2] = b[3] = 0;
			}
		}
		point = 0;
		out = 0;
		b[1] = b[2] = b[3] = 0;
	}
}

C++: b,o,s;main(a){for(scanf("%d");~scanf("%s",&a);)
        {
                if(a%3){
                        if(a&1,++o){
                                if(o%3){
                                        b=!printf("%d\n",s+b>3?b-3:0),s=0;
                                }
                        }
                        else{
                                b++;
                        }
                }
                else{
                        s+=b,b=0;
                }
        }
}

C++: #include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int n, o, h, t;
    string s;
    cin >> n;
     
    while(n--)
	{
        o = h = t = 0;
        while(o < 3)
		{
            cin >> s;
            if(s == "HIT")
			{
                if(h == 3)
				{
                    t++;
                } 
				else 
				{
                    h++;
                }
            } 
			else if(s == "HOMERUN")
			{
                t = t + h + 1;
                 h=0;
            } 
			else 
			{
                o++;
            }
        }
        cout << t << endl;
    }
     
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    while(--n >= 0){
        int ret = 0;
        int out = 0;
        bool first = false;
        bool second = false;
        bool third = false;

        for(;;){
            string s;
            cin >> s;
            if(s == "HIT"){
                if(third)
                    ++ ret;
                third = second;
                second = first;
                first = true;
            }else if(s == "HOMERUN"){
                if(first)
                    ++ ret;
                if(second)
                    ++ ret;
                if(third)
                    ++ ret;
                ++ ret;
                first = second = third = false;
            }else{
                ++ out;
                if(out == 3)
                    break;
            }
        }
        cout << ret << endl;
    }
}

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

int main(){
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    vector<int> base(3,0);
    int res=0;
    int out=0;
    while(out!=3){
      string s;
      cin>>s;
      if(s=="HIT"){
	res+=base[2];
	base[2]=base[1];
	base[1]=base[0];
	base[0]=1;
      }
      if(s=="OUT"){
	out++;
      }
      if(s=="HOMERUN"){
	res+=base[0]+base[1]+base[2];
	for(int i=0;i<3;i++) base[i]=0;
      }

    }
    cout<<res<<endl;

  }
  return 0;
}

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

int main() {
  int n,first,second,third,point,out;
  string str;
  while(cin >> n) {
    point = 0;
    out = 0;
    first = 0;
    second = 0;
    third = 0;
    for(int i=0;i<n;i++) {
      cin >> str;
      if(str == "HIT") {
	if(third == 1) {
	  third = 0;
	  point++;
	}
	if(second == 1) {
	  second = 0;
	  third = 1;
	}
	if(first == 1) {
	  second = 1;
	}
	first = 1;
      }
      else if(str == "OUT") {
	out++;
	if(out == 3) break;
      }
      else if(str == "HOMERUN") {
	point += 1 + first + second + third;
	first = 0;
	second = 0;
	third = 0;
      }
    }
    cout << point << endl;
  }
}