#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
using namespace std;
const int maxn=30;
typedef struct
{
int row;
int col;
}Matrix;
Matrix m[30];
int n;
char stra[10*maxn];
int Solve()
{
stack<char> par;
stack<Matrix> ms;
int len=strlen(stra);
Matrix m1,m2,m3;
int ans=0;
for(int i=0;i<len;i++)
{
if(stra[i]==')')
{
m2=ms.top();
ms.pop();
m1=ms.top();
ms.pop();
if(m1.col!=m2.row)
return -1;
else
{
ans+=(m1.row*m1.col*m2.col);
m3.row=m1.row; m3.col=m2.col;
ms.push(m3);
}
}
else if(stra[i]=='(')
continue;
else
ms.push(m[stra[i]-'A']);
}
return ans;
}
int main(void)
{
//freopen("f:\\file\\data.txt","r",stdin);
scanf("%d",&n);
while(n--)
{
char ch;
int row,col;
scanf("\n%c%d%d",&ch,&row,&col);
m[ch-'A'].row=row;
m[ch-'A'].col=col;
}
while(scanf("%s",stra)==1)
{
int ans=Solve();
if(ans<0)
printf("error\n");
else
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
#include<string.h>
int a[26][2];
int zhan[1000][2];
int top,sum;
int jin(int x)
{
zhan[top][0]=a[x][0];
zhan[top][1]=a[x][1];
top++;
return 0;
}
int chu()
{
if(zhan[top-2][1]!=zhan[top-1][0])return 1;
sum+=zhan[top-1][0]*zhan[top-1][1]*zhan[top-2][0];
zhan[top-2][1]=zhan[top-1][1];//关键把两个栈合并成一个
top--;//栈数减少一个
return 0;
}
int main()
{
//freopen("input.txt","r",stdin);
int n,i,x,y,len,p;
char str[1001];
char ch;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
scanf("%c%d%d",&ch,&x,&y);
a[ch-'A'][0]=x;
a[ch-'A'][1]=y;
getchar();
}
getchar();
while(gets(str))
{
p=0;
top=0,sum=0;
len=strlen(str);
if(len==1)//只有一个输出0
{
printf("0\n");
continue;
}
for(i=0;i<len&&!p;i++)
{
if(str[i]=='(')continue;//(不做处理
if(str[i]==')')p=chu();//)出栈
else jin(str[i]-'A');
}
if(p)
printf("error\n");
else
printf("%d\n",sum);
}
return 0;
}