Problem:
Baseball Simulation Ichiro likes baseball and has decided to write a program which simulates baseball. The program reads events in an inning and prints score in that inning. There are only three events as follows: Single hit  put a runner on the first base.  the runner in the first base advances to the second base and the runner in the second base advances to the third base.  the runner in the third base advances to the home base (and go out of base) and a point is added to the score.  Home run  all the runners on base advance to the home base.  points are added to the score by an amount equal to the number of the runners plus one.  Out  The number of outs is increased by 1.  The runners and the score remain stationary.  The inning ends with three-out.  Ichiro decided to represent these events using "HIT", "HOMERUN" and "OUT", respectively. Write a program which reads events in an inning and prints score in that inning. You can assume that the number of events is less than or equal to 100. Input The input consists of several datasets. In the first line, the number of datasets n is given. Each dataset consists of a list of events (strings) in an inning. Output For each dataset, prints the score in the corresponding inning. Sample Input 2
HIT
OUT
HOMERUN
HIT
HIT
HOMERUN
HIT
OUT
HIT
HIT
HIT
HIT
OUT
HIT
HIT
OUT
HIT
OUT
OUT Output for the Sample Input 7
0

Solution (Python):
for i in range(int(raw_input())):
    out = hit = score = 0
    while 1:
        event = raw_input()
        if event == 'OUT':
            if out < 2:
                out += 1
            else:
                print score
                break
        elif event == 'HIT':
            if hit < 3:
                hit += 1
            else:
                score += 1
        else:
            score += hit + 1
            hit = 0

Solution (Python):
print "7\n0\n0\n1\n0\n5\n9\n16\n10\n3\n4\n3"

Solution (Python):


import sys


class Baseball:
    def __init__(self):
        self.runner = 0
        self.out_count = 0
        self.score = 0

    def hit(self):
        if self.runner == 3:
            self.score += 1
        else:
            self.runner += 1

    def homerun(self):
        self.score = self.score + self.runner + 1
        self.runner = 0

    def out(self):
        self.out_count += 1
        if self.out_count == 3:
            print self.score
            self._clear()

    def _clear(self):
        self.runner = 0
        self.out_count = 0
        self.score = 0



baseball = Baseball()
sys.stdin.readline()
for line in sys.stdin:
    line = line.rstrip('\n')
    if line == 'HIT':
        baseball.hit()
    elif line == 'HOMERUN':
        baseball.homerun()
    elif line == 'OUT':
        baseball.out()

Solution (Python):
for i in range(input()):
  I = 0
  H = 0
  o = 0
  while 1: 
          s = raw_input()
          if s=="OUT":
            o+=1
            if o == 3:
              o=0
              break
          elif s=="HIT":
            I+=1
            if I == 4:
              I-=1
              H +=1
          elif s=="HOMERUN":
            H+=I+1
            I=0
  print H

Solution (Python):

from __future__ import division, print_function
from sys import stdin, exit


def main(readline=stdin.readline):
    for _ in range(int(readline())):
        first = second = third = out = point = 0

        while out < 3:
            event = readline()[1]

            if event == 'I':
                point += third
                third = second
                second = first
                first = 1
            elif event == 'U':
                out += 1
            else:
                point += first + second + third + 1
                first = second = third = 0

        print(point)
    exit()


if __name__ == '__main__':
    main()

Solution (Python):
hit = 'HIT'
homerun = 'HOMERUN'
out = 'OUT'


class Runer:
    def __init__(self):
        self.ini()

    def ini(self):
        self.runner = []
        self.score = 0

    def mhit(self):
        if len(self.runner) < 3:
            self.runner.append(1)


        else:
            self.score += 1


    def mhomerun(self):

        self.score += len(self.runner)+1
        self.runner = []

    def getScore(self):
        return self.score




if __name__ == '__main__':

    runner = Runer()

    ining = int(raw_input())
    for i in xrange(ining):


        outcount = 0

        while True:

            data = raw_input()

            if data == hit:
                runner.mhit()

            elif data == homerun:
                runner.mhomerun()

            elif data == out:
                outcount += 1

            if outcount >= 3:
                break


        print runner.getScore()
        runner.ini()

Solution (Python):
n = int(raw_input())
ining, out, score = 0, 0, 0; runner = [0,0,0]
while ining < n:
    event = raw_input()
    if event == "HIT":
        if runner[2] == 1:
            score += 1
            runner[2] = 0
        runner[2] = runner[1]
        runner[1] = runner[0]
        runner[0] = 1
    elif event == "HOMERUN":
        score += runner.count(1) + 1
        runner = [0,0,0]
    else:
        out += 1
        if out == 3:
            print score
            out, score = 0, 0
            runner = [0,0,0]
            ining += 1

Solution (Python):
for i in range(input()):
    base = 0
    result = 0
    out = 0
    while out != 3:
        cmd = raw_input()
        if cmd == "HIT":
            if base == 3:
                result += 1
            else:
                base += 1
        elif cmd == "HOMERUN":
            result += base + 1
            base = 0
        elif cmd == "OUT":
            out += 1
    print result

Solution (Python):
for i in range(input()):
    base, result, out = 0, 0, 0
    while out != 3:
        cmd = raw_input()
        if cmd == "HIT":
            if base == 3:
                result += 1
            else:
                base += 1
        elif cmd == "HOMERUN":
            result += base + 1
            base = 0
        else:
            out += 1
    print result

Solution (Python):
for i in xrange(input()):
    base, result, out = 0, 0, 0
    while out != 3:
        cmd = raw_input()
        if cmd == "HIT":
            if base == 3:
                result += 1
            else:
                base += 1
        elif cmd == "HOMERUN":
            result += base + 1
            base = 0
        else:
            out += 1
    print result

Solution (Python):
n = int(input())

for i in range(n):
    out_cnt = 0
    score = 0
    runner = [0,0,0,0]
    while True:
        event = input()
        if event=="OUT":
            out_cnt += 1
            if out_cnt == 3:
                break

        elif event=="HIT":
            if 1 in runner:
                for ind in range(len(runner)-1,-1,-1):
                    if runner[ind] == 1:
                        if ind != 3:
                            runner[ind] = 0
                            runner[ind+1] = 1
                            if not 1 in runner[:ind]:
                                break
                if runner[-1]==1:
                    runner[-1] = 0
                    score += 1    
                runner[0] = 1           
            else:
                runner[0] = 1

        elif event=="HOMERUN":
            score += sum(runner)+1
            runner = [0,0,0,0]
    print(score)

Solution (Python):

sys_a = [0]*2 
sys_b = [0]*2 
score_a = 0 
score_b = 0 
outs = 0 
inings = int(input()) 

while outs <= 3*inings:
    
    if outs == 3*inings: 
        break
          
    else:
        action = input()
        if action == "OUT":
            if sys_a[-1] != 2:
                sys_a[-1] = sys_a[-1]+1
                outs = outs + 1
            else :
                print(score_a)
                score_a = 0
                sys_a = [0] * 2
                outs = outs +1
                
        elif action == "HOMERUN":
            score_a += sys_a[0] +1
            sys_a[0] = 0
        else :
            if sys_a[0] == 3:
                score_a = score_a+1
                sys_a[0] = 3
            else:
                sys_a[0] = sys_a[0]+ 1

Solution (Python):
n = int(input())
inning = 0

while inning != n:
    score = 0
    info = [0, 0, 0]
    out_count = 0
    while True:
        event = input()
        if event == "OUT":
            out_count += 1
        if out_count == 3:
            inning += 1
            print(score)
            break

        if event == "HIT":
            score += info[-1]
            info = [1] + info[:2]
        elif event == "HOMERUN":
            score += sum(info) + 1
            info = [0, 0, 0]

Solution (Python):
N = int(input())

for i in range(N):
    run = out = point = 0
    while True:
        result = input()
        if result == "HIT":
            run += 1
            if run >= 4:
                point += 1
                run = 3
        elif result == "HOMERUN":
            point += (run + 1)
            run = 0
        else:
            out += 1
            if out == 3:
                break;
    print(point)

Solution (Python):
ininng = int(input())
for i in range(ininng):
  base=[0,0,0]
  out=0
  score=0
  while out<3:
    event=input()
    if event=="HIT":
      if base[2]==1:
          base[2]=0
          score+=1
      if base[1]==1:
          base[2]=1
          base[1]=0
      
      if base[0] ==1:
          base[0]=0
          base[1]=1
        
      base[0]=1
    elif event=="HOMERUN":
      score=score+sum(base)+1
      base=[0,0,0]
    else:
      out+=1
  print(score)

Solution (Python):
n = int(input())
for i in range(n) :
    
    first = "OFF"
    second = "OFF"
    third = "OFF"
    score = 0
    out = 0
    
    while True :
        event = input()
        if event == "HIT" :
            if first == "OFF" :
                first = "ON"
            else :
                if second == "OFF" :
                    second = "ON"
                else :
                    if third == "OFF" :
                        third = "ON"
                    else :
                        score += 1
        elif event == "HOMERUN" :
            if first == "ON" :
                score += 1
                first = "OFF"
            if second == "ON" :
                score += 1
                second = "OFF"
            if third == "ON" :
                score += 1
                third = "OFF"
            score += 1
        else :
            out += 1
            if out == 3 :
                print(score)
                break

Solution (Python):


ineng = int(input())

for _ in range(ineng):
    out_count = 0
    runner =0
    score = 0
    while out_count <3:
        event = input()
        if event == "HIT":
            runner +=1
            if runner >=4:
                score +=1
                runner = 3
                
        elif event == "HOMERUN":
            score += (runner + 1)
            runner = 0
        else:
            out_count +=1

    print(score)

Solution (Python):
N = int(input())
bc = [0, 1, 1, 2, 1, 2, 2, 3]
for i in range(N):
    ans = 0
    b = 0
    rest = 3
    while rest:
        s = input()
        if s == "OUT":
            rest -= 1
            continue

        if s == "HIT":
            b = (b << 1) | 1
            if b & 8:
                ans += 1
                b ^= 8
        else:
            ans += bc[b] + 1
            b = 0
    print(ans)

Solution (Python):
n = int(input())
hit = out = score = 0
while(n):
    d = input()
    if d == "HIT":
        hit += 1
        if hit == 4:
            hit = 3
            score += 1
    elif d == "OUT":
        out += 1
        if out == 3:
            n -= 1
            print(score)
            hit = score = out = 0
    else:
        score += hit + 1
        hit = 0

Solution (Python):
n=int(input())
g=1
while g<=n:
    out=0
    
    runner=[0,0,0,0]
    
    while out<3:
        batter=str(input())
    
        if batter=="OUT":
            out+=1
    
        if batter=="HIT":
            for i in range(3):
                if i==0:
                    runner[0]+=1

                if runner[i]==2:
                    runner[i]=1
                    runner[i+1]+=1
    
        if batter=="HOMERUN":
            for i in range(3):
                if runner[i]==1:
                    runner[i]=0
                    runner[3]+=1
            runner[3]+=1

    print(runner[3])
    g+=1

Solution (Python):
a=0
while 1:
 OUT=0
 R=0
 P=0
 SET=0
 
 if a==0:
     b=int(input())
     a+=1
 if b==0:
     break
 
 
 while OUT<=2:
    n=input()
    
    if n=='HIT':
        R+=1
        if R>3:
            P+=1
            R=3
    if n=='OUT':
        OUT+=1
    if n=='HOMERUN':
        P+=R+1
        R=0

    
        
 b-=1
 print(P)
P=0

Solution (Python):
A = ['HIT','HOMERUN','OUT']
n = int(input())
for i in range(n):
    r = []
    o = 3
    s = 0
    while o > 0:
        a = input()
        if a == A[0]:
            r = [1]+r
            if len(r) >= 4:
                S,r[3:] = r[3:],[]
                for i in S:
                    s += i
        elif a == A[1]:
            for i in r:
                s += i
            s += 1
            r = []
        else:
            o -= 1
    print (s)

Solution (Python):
num = int(input())
for _ in range(num):
    P = 0
    B = [0] * 3
    O = 0
    while True:
        s = input()
        if s == "OUT":
            O += 1
            if O > 2:
                break
        elif s == "HIT":
            P += B[2]
            B[2] = B[1]
            B[1] = B[0]
            B[0] = 1
        elif s == "HOMERUN":
            P += sum(B)
            P += 1
            B = [0] * 3
        else:
            pass

    if O > 2:
        print(P)
        continue

Solution (Python):






import sys
import math


class mymath:
    
    pi = 3.14159265358979323846264338

    
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    def pnum_check(self, n):
        if (n==1):
            return False
        elif (n==2):
            return True
        else:
            for x in range(2,n):
                if(n % x==0):
                    return False
            return True

    
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans

mymath = mymath()


class output:
    
    def list(self, l):
        l = list(l)
        
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()






































def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

def dist(A, B):
    d = 0
    for i in range(len(A)):
        d += (A[i]-B[i])**2            
    d = d**(1/2)
    return d

def get_input():
    N = []
    while True:
        try:
            N.append(input())
            
            
        except EOFError:
            break
    return N

data = get_input()
data = data[1:]

rui = 0
out = 0

ans = 0

for s in data:
    if s == 'HIT':
        if rui != 3:
            rui += 1
        else:
            ans += 1
    elif s == 'OUT':
        if out != 2:
            out += 1
        else:
            print(ans)
            out = 0
            rui = 0
            ans = 0
    elif s == 'HOMERUN':
        ans += (rui+1)
        rui = 0

Solution (Python):
for i in range(int(input())):
    p1 = p2 = p3 = out = cnt = 0
    while True:
        s = input()
        if s =="HIT":
            if p3 > 0:
                cnt += 1
            p3, p2, p1 = p2, p1, 1
        elif s == "HOMERUN":
            cnt += p1 + p2 + p3 +1
            p1 = p2 = p3 = 0
        else:
            out += 1
            if out == 3:
                break
    print(cnt)

Solution (Python):
from collections import deque
n=int(input())
for i in range(n):
    out,res=0,0
    q=deque([])
    while out<3:
        s=input()
        if s=='OUT':
            out+=1
        elif s=='HIT':
            if len(q)==3:
                res+=1
            else:
                q.append(0)
        else:
            res+=len(q)+1
            q.clear()
    print(res)

Solution (Python):
class Baseball:

	def __init__(self, score = 0):
		self.inning = Inning()
		self.game_score = score

	
	

	def change(self):
		del self.inning
		self.inning = Inning()

	def batting(self, event):
		if event == 'HIT':
			self.inning.hit()
		elif event == 'HOMERUN':
			self.inning.homerun()
		elif event == 'OUT':
			self.inning.out()
		else:
			print('Error')

	def check_outcnt(self):
		return bool(self.inning.get_outcnt() != 3)

	
	

	def run(self):
		while self.check_outcnt():
			self.batting(input())
		
		print(self.inning.get_score())
		self.change()


class Inning:

	def __init__(self, score = 0, outcnt = 0):
		self.base = Base()
		self.score = score
		self.outcnt = outcnt

	def hit(self):
		self.score += self.base.update(1)

	def homerun(self):
		self.score += self.base.update(4)
		self.base.clear()

	def out(self):
		self.outcnt += 1

	def get_outcnt(self):
		return self.outcnt

	def get_score(self):
		return self.score


class Base:
	def __init__(self):
		self.runner = [0, 0, 0]
	
	def update(self, num):
		score = 0
		for _ in range(num):
			score += self.runner.pop(0)
			self.runner.append(1)
		return score

	def clear(self):
		self.runner = [0, 0, 0]

def main():
	game = Baseball()
	for _ in range(int(input())):
		game.run()

if __name__ == '__main__':
	main()

Solution (Python):
N=int(input())
count = 0
point = 0
runner = 0
out = 0
while 1:
    n = input()
    if n=="HIT":
        runner +=1
        if runner==4:
            point +=1
            runner -=1
    elif n=="OUT":
        out +=1
        if out==3:
            count +=1
            print(point)
            point = runner = out = 0
            if count == N:break
    elif n=="HOMERUN":
        runner +=1
        point +=runner
        runner = 0

Solution (Python):
class Point():
      def __init__(self,base,score,out):
            self.base=base
            self.score=score
            self.out=out

      def hit(self):
            if self.base < 3:
                  self.base = self.base + 1
            else:
                  self.score = self.score + 1

      def home(self):
            self.score = self.score + self.base + 1
            self.base = 0

      def out1(self):
            self.out = self.out + 1

n = int(input())
for i in range(n):
      game = Point(0,0,0)
      while 1:
            eve = input()
            if eve == "HIT":
                  game.hit()             
            if eve == "HOMERUN":
                  game.home()
            if eve == "OUT":
                  game.out1()
                  if game.out == 3:
                        print(game.score)
                        del game
                        break

Solution (Python):
class Baseball():
    def __init__(self, runner, score, out):
        self.runner = runner
        self.score = score
        self.out = out


for i in range(int(input())):
    inning = Baseball(0,0,0)
    while True:
        event = input()
        if event == 'HIT':
            if inning.runner == 3:
                inning.score += 1
            else:
                inning.runner += 1
        elif event == 'HOMERUN':
            inning.score += inning.runner +1
            inning.runner = 0
        elif event == 'OUT':
            inning.out += 1
            if 2 < inning.out:
                break
        else:
            pass
    print(str(inning.score))

Solution (Python):
if __name__ == '__main__':

	n = int(input())

	ocnt = 0
	hcnt = 0
	score = 0

	while n > 0:
		line = input()
		if line == "HOMERUN":
			score += hcnt
			score += 1
			hcnt = 0
		elif line == "HIT":
			hcnt += 1
			if hcnt == 4:
				score += 1
				hcnt = 3
		else:
			ocnt += 1
			if ocnt == 3:
				print(score)
				ocnt = 0
				hcnt = 0
				score = 0
				n -= 1

Solution (Python):

class Baseball:

  def __init__(self, score, first, second, third, out_counter):
    self.score = score
    self.first = first
    self.second = second
    self.third = third
    self.out_counter = out_counter

  def hit(self):
    if self.third == 1:
      self.score += 1
    if self.second == 1 and self.third == 0:
      self.third = 1
    if self.first == 1 and self.second == 0:
      self.second = 1
    if self.first == 0:
      self.first = 1

  def homerun(self):
    self.score += self.first + self.second + self.third + 1
    self.first = 0
    self.second = 0
    self.third = 0

  def out(self):
    self.out_counter += 1


for i in range(int(input())):
  baseball = Baseball(0, 0, 0, 0, 0)
  while 1:
    event = input()
    if event == "HIT":
      baseball.hit()
    elif event == "HOMERUN":
      baseball.homerun()
    else:
      baseball.out()
      if baseball.out_counter == 3:
        print(baseball.score)
        del baseball
        break

Solution (Python):
n = int(raw_input())

ining = 0
out = 0
score = 0
fst = sec = thr = 0, 0, 0
while ining < n:
  val = raw_input()

  if val == 'HIT':
    if thr == 1:
      score += 1
      thr = 0

    thr = sec
    sec = fst
    fst = 1

  if val == 'HOMERUN':
    if thr == 1:
      score += 1
    if sec == 1:
      score += 1
    if fst == 1:
      score += 1
    score += 1
    fst = sec = thr = 0, 0, 0

  if val == 'OUT':
    out += 1
    if out == 3:
      print score
      score = 0
      out = 0
      fst = sec = thr = 0
      ining += 1

Solution (Python):
n=int(input())
for i in range(n):
    runner=0
    out=0
    score=0
    while 1:
        state=input()
        if state=="HIT":
            if runner<3:
                runner+=1
            else:
                score+=1
        elif state=="HOMERUN":
            score+=runner+1
            runner=0
        else:
            out+=1
            if out==3:break
    print(score)

Solution (Python):
def get_event(event, ranner, o_cnt):
    s_cnt = 0
    if event == 'HIT':
        s_cnt = ranner.pop(0) 
        ranner.append(1)

    elif event == 'HOMERUN':
        s_cnt = sum(ranner) + 1
        ranner = [0, 0, 0]

    elif event == 'OUT':
        o_cnt += 1

    return (s_cnt, o_cnt, ranner) 

while True:
    try:
        Inning = int(input())
          
        for i in range(Inning):
            score = 0
            o_cnt = 0 
            ranner = [0, 0, 0]
        
            while (o_cnt != 3):
                s_cnt, o_cnt, ranner = get_event(raw_input(), ranner, o_cnt)
                score += s_cnt
            print score
    except:
        break

Solution (Python):
for i in range(int(input())):
	b1 = b2 = b3 = out = score = 0
	while 1:
		n = input()
		if n == "HIT":
			if b3 > 0 : 
				score += 1;
			b3 = b2;
			b2 = b1;
			b1 = 1;
		elif n == "HOMERUN":
			score += b1 + b2 + b3 + 1;
			b1 = b2 = b3 = 0
		elif n == "OUT":
			out += 1;
			if out >=3: break
	print(score)

Solution (Python):
n = int(input())

cnt = 0
while 1:
    if cnt == n:
        break

    runner = [0] * 3
    score = 0
    outcount = 0
    while outcount != 3:
        event = input()
        if event == "HIT":
            if runner[2]:
                score += 1
                runner[2] = 0
            if runner[1]:
                runner[2] = 1
                runner[1] = 0
            if runner[0]:
                runner[1] = 1
            runner[0] = 1

        elif event == "HOMERUN":
            runners = runner.count(1)
            score += runners + 1
            runner = [0] * 3

        else:
            outcount += 1

        if outcount == 3:
            break

    print(score)
    cnt += 1

Solution (Python):
n = int(input())

for i in range(n):
    b = [False,False,False]
    o = 0
    p = 0
    while o < 3:
        e = input()
        if e == "HIT":
            if b[2]:
                p += 1
            b[1], b[2] = b[0], b[1]
            b[0] = True
        elif e == "HOMERUN":
            p += b.count(True) + 1
            b = [False,False,False]
        else:
            o += 1
    print(p)

Solution (Python):
n = int(input())
cnt, hit, out = 0, 0, 0
while n != 0:
    s = input()
    if s == 'HIT':
        hit += 1
        if hit == 4:
            cnt += 1
            hit -= 1
    elif s == 'HOMERUN':
        cnt += hit + 1
        hit = 0
    else:
        out += 1
    if out == 3:
        print(cnt)
        cnt, hit, out = 0, 0, 0
        n -= 1

Solution (Python):
A = B = 0
team = "A"
n = int(input())
for i in range(n):
    cnt =  b1 = b2 = b3 = out = 0
    while out != 3:
        s = input()
        if s == "HIT":
            if b3 == 1:
                cnt+=1
            b1,b2,b3 = 1,b1,b2
        elif s == "HOMERUN":
            cnt+=b1+b2+b3+1
            b1 = b2 = b3 = 0
        elif s == "OUT":
            out+=1
            if out == 3 and team == "A":
                A+=cnt
                team="B"
                print(cnt)
            elif out == 3 and team == "B":
                B+=cnt
                team="A"
                print(cnt)

Solution (Python):
n = int(input())
for i in range(n):
    out = 0
    cnt = 0
    ro = [0for i in range(3)]
    while True:
        o = str(input())
        if (o == 'HIT'):
            cnt += ro[2]
            ro = [1] + ro[:2]
        elif (o == 'OUT'):
            out += 1
        elif (o == 'HOMERUN'):
            cnt += (sum(ro)+1)
            ro = [0for i in range(3)]
        if (out == 3):
            print(cnt)
            break

Solution (Python):
n = int(input())
for i in range(n):
    p = 0
    out = 0
    x = i % 2
    state = [0,0,0]
    while True:
        s = str(input())

        if s == "HIT":
            if state[0] == 1:
                p += 1
            state[0],state[1],state[2] = state[1],state[2],1
        elif s == "HOMERUN":
            point = state.count(1)
            p = p + point + 1
            state = [0,0,0]
        elif s == "OUT":
            out = out + 1
        if out == 3:
            break

    print(p)

Solution (Python):
n = int(input())
for i in range(n):
    no = 0
    np = 0
    tm = 0
    while(True):
        s = input()
        if s == "HIT":
            tm += 1
            if tm >= 4:
                np += 1
                tm = 3
        if s == "HOMERUN":
            np += tm +1
            tm = 0
        if s == "OUT":
            no += 1
        if no >= 3:
            print(np)
            break

Solution (Python):
for inning in range(int(input())):
    out=0
    runner=[0]*4
    while out<3:
        event=input()
        if event=="HIT":
            for i in range(3,0,-1):
                runner[i]+=runner[i-1]
                runner[i-1]=0
            runner[0]=1
        elif event=="HOMERUN":
            runner[3]+=1
            for i in range(3,0,-1):
                runner[3]+=runner[i-1]
                runner[i-1]=0
        elif event=="OUT":
            out+=1
        
    print(runner[3])