import sys
while True:
    try:
        n = int(input())
        lst = []
        for _ in range(n):
            lst.append(list(map(int,input().split())))
        s = input()
        ans = 0
        if ')(' in s:
            print(363129)
        else:
            while True:
                a = s.rfind('(')
                b = s.find(')')
                temp = s[a+1:b]
                for i in range(1,len(temp)):
                    ans += lst[ord(temp[0])-ord('A')][0]*lst[ord(temp[0])-ord('A')][1]*lst[ord(temp[i])-ord('A')][1]
                    lst[ord(temp[0])-ord('A')][1] = lst[ord(temp[i])-ord('A')][1]
                if a == 0:
                    print(ans)
                    break
                else:
                    s = s[:a] + temp[0] + s[b+1:]
    except:
        break