/*Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))
Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125*/
#include <iostream>
#include <stack>
#include<vector>
#include<string>
#include<map>
using namespace std;
struct ju{
int z,y;
ju(int a=0,int b=0)
{
z=a;
y=b;
}
};
ju m[26];
stack <ju> j;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
string name;
cin>>name;
int k=name[0]-'A';
cin>>m[k].z>>m[k].y;
}
string s;
while(cin>>s)
{
int sum=0,flag=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='(')
continue;
else if(isalpha(s[i]))
{
j.push(m[s[i]-'A']);
}
else if(s[i]==')')
{
ju t2=j.top();
j.pop();
ju t1=j.top();
j.pop();
if(t1.y!=t2.z)
{
cout<<"error"<<endl;
flag=1;
break;
}
else{
j.push(ju(t1.z,t2.y));
sum+=(t1.z*t2.z*t2.y);
}
}
}
if(!flag)
cout<<sum<<endl;
}
// cout << "Hello world!" << endl;
return 0;
}