#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
struct Matrix
{
int h,l;
Matrix(int h=0,int l=0):h(h),l(l){}
} m[26];
int n;
stack<Matrix> s;
int main()
{
cin>>n;
int i;
for(i=0;i<n;i++)
{
string name;
cin>>name;
int k=name[0]-'A';
cin>>m[k].h>>m[k].l;
}
string str;
while(cin>>str)
{
int len=str.length();
bool error=false;
int ans=0;
for(i=0;i<len;i++)
{
if(isalpha(str[i])) s.push(m[str[i]-'A']);
else if(str[i]==')')
{
Matrix m2=s.top();s.pop();
Matrix m1=s.top();s.pop();
if(m1.l!=m2.h) {error=1;break;}
ans+=m1.h*m1.l*m2.l;
s.push(Matrix(m1.h,m2.l));
}
}
if(error)
cout<<"error"<<endl;
else
cout<<ans<<endl;
}
return 0;
}
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
struct Matrix
{
int h,l;
Matrix(int h=0,int l=0):h(h),l(l){}
} m[26];
int n;
stack<Matrix> s;
int main()
{
cin>>n;
int i;
for(i=0;i<n;i++)
{
string name;
cin>>name;
int k=name[0]-'A';
cin>>m[k].h>>m[k].l;
}
string str;
while(cin>>str)
{
int len=str.length();
bool error=false;
int ans=0;
for(i=0;i<len;i++)
{
if(isalpha(str[i])) s.push(m[str[i]-'A']);
else if(str[i]==')')
{
Matrix m2=s.top();s.pop();
Matrix m1=s.top();s.pop();
if(m1.l!=m2.h) {error=1;break;}
ans+=m1.h*m1.l*m2.l;
s.push(Matrix(m1.h,m2.l));
}
}
if(error)
cout<<"error"<<endl;
else
cout<<ans<<endl;
}
return 0;
}