


inp=open('DATA5.txt')
out=open('OUT5.txt','w')

for j in range(5):
    lines=int(inp.readline())
    ans=100000000000000000
    total=0
    e=''
    for i in range(lines):
        d=inp.readline().split()
        if d[0]=="YYZ":
            total+=int(d[2])
        if e==d[0]:
            total+=int(d[2])
        e=d[1]
        if d[1]=="SEA":
            if total<ans: ans=total
            total=0
            e=''
    out.write(str(ans)+'\n')
out.close()
inp.close()

