//到现在使用最多的map映射到vector map set 作为哈希表
//set查重,用vector保存顺序
//vector保存数据分配数字标记
//自定义struct打包pair和某些属性,记得初始化方法
//stack处理后进先出,字符串
//string和vector连用,保存数据,获得一行和一个字符串再for遍历
//queue保存队列,先进先出,一般用deque代替她的功能,自定义优先级用优先队列
//建立数据结构方法类型
#include<iostream>
#include<stack>#include<cstdio>
using namespace std;
struct m{
int b,c;
m(int b=0,int c=0):b(b),c(c){}
}mat[26];
int main(){
int a;
string name;
cin>>a;
for(int i=0;i<a;i++){
cin>>name;
cin>>mat[name[0]-'A'].b>>mat[name[0]-'A'].c;
}
while(cin>>name){
stack<m> stk;
long long num=0;
int error=0;
for(int i=0;i<name.size();i++){
if(isalpha(name[i]))
stk.push(mat[name[i]-'A']);
if(name[i]==')'){
m m1=stk.top();stk.pop();
m m2=stk.top();stk.pop();
if(m2.c!=m1.b){
error=1;
break;
}
num+=m2.b*m2.c*m1.c;
stk.push(m(m2.b,m1.c));
}
}
if(error) printf("error\n");
else printf("%d\n",num);
}
return 0;
}