while True:
    try:
        num = int(input())
        matrix = []
        for i in range(num):
            matrix.append(list(map(int,input().split())))
        a = list(input())
        b = []
        sum_num = 0
        for i in range(len(a)):
            if(a[i].isupper()):
                b.append(a[i])
            if(a[i] == ')'):
                matrix_1 = b[-2]
                del(b[-2])
                matrix_2 = b[-1]
                del(b[-1])
                num_1 = 0
                num_2 = 0
                if(matrix_1.isupper()):
                    matrix_1 = ord(matrix_1) - ord('A')
                    num_1 = matrix[matrix_1][0] * matrix[matrix_1][1]
                    input_1 = matrix[matrix_1][0]
                else:
                    matrix_1 = matrix_1.split()
                    num_1 = int(matrix_1[0])
                    input_1 = num_1
                    num_1 = int(matrix_1[0]) * int(matrix_1[1])
                if(matrix_2.isupper()):
                    matrix_2 = ord(matrix_2) - ord('A')
                    num_2 = matrix[matrix_2][1]
                else:
                    matrix_2 = matrix_2.split()
                    num_2 = int(matrix_2[1])
                c = num_1 * num_2
                b.append(str(input_1) +' ' + str(num_2))
                sum_num += num_1 * num_2
        print(sum_num)
    except:
        break