#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
class matrix{
public:
char name;
int row;
int rank;
matrix(){
name = ' ';
row = 0;
rank = 0;
}
};
int main(){
vector<matrix>a;
int b;
cin >> b;
while(b>0){
matrix c;
cin >> c.name;
cin >> c.row;
cin >> c.rank;
a.push_back(c);
b--;
}
string s = " ";
char d;
int i = 0;
int number = 0;
stack<char> e;
cin >> s;
for(unsigned int j = 0;j<s.length();j++){
d = s[j];
if(d=='(')
continue;
else if(d==')'){
char f = e.top();
e.pop();
char g = e.top();
e.pop();
int m1 = 0,n2 = 0,n = 0;
vector<matrix>::iterator it;
for(it=a.begin();it!=a.end();it++){
if(it->name==g){
m1 = it->row;
n = it->rank;
}
else if(it->name==f){
n2 = it->rank;
}
}
number += m1*n2*n;
matrix h;
h.name = i;
h.row = m1;
h.rank = n2;
e.push(h.name);
a.push_back(h);
i++;
}
else{
e.push(d);
}
}
if(e.size()==2){
char f = e.top();
e.pop();
char g = e.top();
e.pop();
int m1 = 0,n2 = 0,n = 0;
vector<matrix>::iterator it;
for(it=a.begin();it!=a.end();it++){
if(it->name==g){
m1 = it->row;
n = it->rank;
}
else if(it->name==f){
n2 = it->rank;
}
}
number += m1*n2*n;
}
cout << number;
}