def cal_val(A,B):
    if A[1]==B[0]:
        return A[0]*B[1]*A[1]
    else:
        return -1

def cal(A,B):
    if A[1]==B[0]:
        return [A[0],B[1]]
    else:
        return -1

while True:
    try:
        n=int(input())
        array=[]
        for i in range(n):
            matrix=list(map(int,input().split()))
            array.append(matrix)
        array=array[::-1]
        express=str(input().strip())
        stack=[]
        cal_value=0
        for i in range(len(express)):
            if express[i].isalpha():
                stack.append(array.pop())
            elif express[i]==')':
                if len(stack)>1:
                    A=stack.pop()
                    B=stack.pop()
                    #print('A,B:',A,B)
                    cal_value+=cal_val(B,A)
                    stack.append(cal(B,A))
        stack=stack[::-1]
        while len(stack)>1:
            A=stack.pop()
            B=stack.pop()
            cal_value+=cal_val(A,B)
            stack.append(cal(A,B))
        print(cal_value)
    except:
        break