import java.util.*;
/**
* HJ70 矩阵乘法计算量估算 - 中等
*/
public class HJ070 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
int n = Integer.parseInt(sc.nextLine());
int[][] num = new int[n][2];
for (int i = 0; i < n; i++) {
String[] arr = sc.nextLine().split(" ");
num[i][0] = Integer.parseInt(arr[0]);
num[i][1] = Integer.parseInt(arr[1]);
}
String msg = sc.nextLine();
Stack<Integer> z = new Stack<>();
int count = 0;
for (int j = 0; j < msg.length(); j++) {
char tmp = msg.charAt(j);
if (tmp != '(') {
if (tmp == ')') {
int cc = z.pop();
int cr = z.pop();
int bc = z.pop();
int br = z.pop();
count += br * cc * bc;
z.push(br);
z.push(cc);
} else {
int index = tmp - 'A';
z.push(num[index][0]);
z.push(num[index][1]);
}
}
}
System.out.println(count);
}
sc.close();
}
}