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++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  cin >> n;
  rep(_, n) {
    int a = 0;
    vi x(3);
    int c = 0;
    while (a < 3) {
      string s;
      cin >> s;
      if (s == "OUT") {
        a++;
      }
      if (s == "HOMERUN") {
        c += count(all(x), 1) + 1;
        fill(all(x), 0);
      }
      if (s == "HIT") {
        c += x[2];
        rotate(x.begin(), x.begin() + 2, x.end());
        x[0] = 1;
      }
    }
    cout << c << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
#define FOR(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
const int MAX = 101;

template<class T, class U>
void convert(T &t, U &u){
    stringstream ss;
    ss << t;
    ss >> u;
}

int main(){
	int n;
	cin >> n;
	while(n--){
		bool base[4];
		memset(base, false, sizeof(base));
		int out = 0, point = 0;
		while(1){
			bool base_next[4];
			memset(base_next, false, sizeof(base_next));
			string ev;
			cin >> ev;
			if(ev=="HIT"){
				FOR(i, 3){
					if(base[i]){
						base_next[i+1]=true;
						if(i+1==3) point++;
					}
				}
				base_next[0]=true;
			}else if(ev=="OUT"){
				out++;
				if(out==3) break;
				continue;
			}else{
				FOR(i, 3){
					if(base[i]) point++;
				}
				point++;
			}
			FOR(i, 4){
				base[i] = base_next[i];
			}
		}
		cout << point << endl;
	}
	return 0;
}

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

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

	return 0;
}

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

int main() {
	
	int n;
	
	std::cin >> n;
	
	while(n-->0) {
		int a[3] = { 0, 0, 0};
		int point = 0;
		int out = 0;
		std::string commands = "";
		while(1) {
			std::cin >> commands;

			// HIT
			if( commands == "HIT") {
				if( a[2] == 1) point ++;
				a[2] = a[1];
				a[1] = a[0];
				a[0] = 1;
			}

			// OUT
			if( commands == "OUT") out ++;

			// HOMERUN
			if( commands == "HOMERUN") {
			for( int j=0; j<3; j++)
				if( a[j] == 1) {
					point ++;
					a[j] = 0;
				}
				point ++;
			}

			if(out==3) break;
		}
		
		std::cout << point << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
	int n,a[5];
	string b;
	cin>>n;
	int out;
	for(int i=0;i<n;i++){
		out=0;
		for(int i=0;i<5;i++)
		a[i]=0;
		while(true){
			cin>>b;
			a[0]=1;
			if(b=="HIT"){
				a[4]+=a[3];
				a[3]=a[2];
				a[2]=a[1];
				a[1]=1;
		    }
			if(b=="HOMERUN"){
				a[4]+=a[3]+a[2]+a[1]+1;
				a[3]=0;
				a[2]=0;
				a[1]=0;
			}
			if(b=="OUT"){
				out++;
				if(out==3)
					break;
			}
		}
		cout<<a[4]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int n; cin >> n;
	rep(i, 0, n) {
		int A[4] = {}, OUT = 0, ans = 0;
		for (string s; cin >> s;) {
			if (s == "HOMERUN") {
				ans++;
				rep(j, 0, 3) { ans += A[j]; A[j] = 0; }
			}
			else if (s == "HIT") {
				rrep(j, 1, 4)A[j] = A[j - 1];
				A[0] = 1;
				ans += A[3];
			}
			else {
				OUT++;
				if (OUT >= 3)break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: // This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <stdio.h>
#include <string.h>

int main()
{
int n;
int runner;
int score;
int out;
char nyuuryoku[114514];
    scanf("%d",&n);
        for(int i=0;i<n;i++){
            runner=0;
            score=0;
            out=0;
            while(out<3){
                scanf("%s",nyuuryoku);
                    if(strcmp(nyuuryoku,"HIT")==0){
                        runner=runner+1;
                    }
                    else if(strcmp(nyuuryoku,"HOMERUN")==0){
                        score=score+runner+1;
                        runner=0;
                    }
                    else if(strcmp(nyuuryoku,"OUT")==0){
                        if(out==2&&runner>3){
                            score=score+runner-3;
                        }
                            
                           out=out+1;
                    }
            }
            printf("%d\n",score);
        }
                
}
// GCC reference:
//   https://gcc.gnu.org/

// C++ language references:
//   https://msdn.microsoft.com/library/3bstk3k5.aspx
//   http://www.cplusplus.com/
//   https://isocpp.org/
//   http://www.open-std.org/jtc1/sc22/wg21/

// Boost libraries references:
//   http://www.boost.org/doc/