# 2024年9月23日   上午9:20

n = int(input())
ls = []
for i in range(n):
    ls.append(list(map(int,input().split())))
#print(ls)
s = input()  # 计算顺序,(A(BC))
stack = []
cc = 0
for i in s:
    if i.isalpha(): # 如果实在看不懂,可以暂时这么理解, 对于(A(BC)),当前i是字母A,需要入栈的数据是ls[0],所以代码是(ord(i)-65)
        stack.append(ls[ord(i)-65])
    elif i == ')' and len(stack) >= 2:
        c = stack.pop()
        b = stack.pop()
        cc += c[0]*c[1]*b[0]
        stack.append([b[0],c[1]])
print(cc)