while True:

try:
    
    n = int(input())
    matrix_list = []
    for i in range(n):
        matrix_list.append([int(i) for i in input().split()])
        
    rule = input()
    
    temp = []
    times = 0
    for i in rule:
        if i.isalpha():
            temp.append(i)
        elif i == ')':
            x = temp.pop()
            y = temp.pop()
            times += matrix_list[ord(y)-65][0] * matrix_list[ord(y)-65][1] * matrix_list[ord(x)-65][1]
            
            matrix_list[ord(y)-65] = (matrix_list[ord(y)-65][0], matrix_list[ord(x)-65][1])
            temp.append(y)
    print(times)
    
except:
    break