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 <cmath>
#include <ctime>
#include <climits>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cstring>
#include <tuple>
#include <bitset>
#include <unordered_map>

using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i=0; i<n; ++i) {
        vector<int> base(3, 0);
        string event;
        int score = 0, out = 0;
        while (cin >> event) {
            if (event == "HIT") {
                score += base[2];
                base[2] = base[1];
                base[1] = base[0];
                base[0] = 1;
            } else if (event == "OUT") {
                out ++;
                if (3 <= out) break;
            } else if (event == "HOMERUN") {
                score += base[2] + base[1] + base[0] + 1;
                fill(base.begin(), base.end(), 0);
            }
        }
        cout << score << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
using namespace std;
int x[4], n, cnt; string S;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++){
		while (true) {
			memset(x, 0, 16);
			if (S == "HIT") {
				x[0] += x[3]; x[3] = 0;
				x[3] += x[2]; x[2] = 0;
				x[2] += x[1]; x[1] = 0;
				x[1] += 1;
			}
			if (S == "OUT") {
				cnt++; if (cnt % 3 == 0) { goto E; }
			}
			if (S == "HOMERUN") {
				x[0] += x[3] + x[2] + x[1] + 1;
				x[3] = 0; x[2] = 0; x[1] = 0; x[0] = 0;
			}
		}
	E:;
		cout << x[0] << endl;
	}
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

int get_score(ll* men){
	int score=0;

	ll tmp = *men;
	tmp >>= 3;
	while(tmp > 0){
		if(tmp | 1){
			score++;
		}
		tmp >>= 1;
	}
	*men = *men & ((1<<3) - 1);

	return score;
}

int main(){

	string str;
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		string str;
		ll men=0;
		int c=0;
		int score=0;
		while(cin >> str){
			if(str == "OUT"){
				c++;
			}
			else if(str == "HIT"){
				men <<= 1;
				men |= 1;

				score += get_score(&men);
			}
			else if(str == "HOMERUN"){
				men <<= 1;
				men |= 1;
				men <<= 3;

				score += get_score(&men);
			}

			if(c==3){
				cout << score << endl;
				c=0;
				score=0;
				men=0;
			}
		}
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>

using namespace std;

typedef  long long int lint;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define mp make_pair
#define enld endl

int main() {
	lint T;
	cin >> T;
	string S;
	lint out = 0;
	vector<lint>A;
	while (cin >> S) {
		if (S == "HIT") {
			rep(i, A.size()) {
				A[i] += 1;
			}
			A.push_back(1);
		}
		else if (S == "OUT") {
			out++;
			if (out == 3) {
				lint cnt = 0;
				rep(i, A.size()) {
					if (A[i] >= 4) {
						cnt++;
					}
				}
				cout << cnt << endl;
				A.clear();
				out = 0;
			}
		}
		else if (S == "HOMERUN") {
			rep(i, A.size()) {
				A[i] += 4;
			}
			A.push_back(4);
		}
	}
}

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

using namespace std;

int main(void){
	int game;
	while(cin >> game){
	for(int i = 0; i < game; i++){
		int out = 0;
		int base = 0;
		int point = 0;
		string n;
		while(out < 3){
			cin >> n;
			if(n == "HIT"){
				base++;
				if(base == 4)
				point++;
			}else if(n == "OUT"){
				out++;
			}else if(n == "HOMERUN"){
				point += base + 1;
			}
		}
		cout << point << endl;
	}
	}
	return 0;
}

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);
        }
    }
}

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){
        switch(event){
            case 'HIT':
                game.hit();
                break;
            case 'HOMERUN':
                game.homerun();
                break;
            case 'OUT':
                game.out();
        }
        if(game.count == 3){
            --n;
            std::cout << game.score << std::endl;
            game = Baseball(0,0);
        }
    }
}

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

int main() {
  int n, s, r, o;
  string e;

  cin >> n;
  for (int i=0; i<n; i++) {
    s = r = o = 0;
    while (1) {
      cin >> e;
      if (e == "HIT") {
        if (++r >= 4) {
          ++s;
          --r;
        }
      } else if (e == "HOMERUN") {
        s += r+1;
        r = 0;
      } else if (e == "OUT") {
        o++;
        if (o >= 3) break;
      }
    }
    cout << s << endl;
  }

  return 0;
}