while True:
    try:
        n =int(input())
        dic = {}
        stk = []
        num = 0
        for i in range(n):
            key = chr(ord('A')+i)
            dic[key] = list(map(int,input().split()))
        x = input()
        for i in range(len(x)):
            if x[i] != ')':
                stk.append(x[i])
            else:
                C,B = stk.pop(),stk.pop()
                stk.pop()
                num += dic[B][0]*dic[B][1]*dic[C][1]
                dic[B] = [dic[B][0], dic[C][1]]
                stk.append(B)
        print(num)
    except:
        break