#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int r=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')r=(r<<3)+(r<<1)+(c^48),c=getchar();
return r*f;
}
void file(string s){
s+=".in";
freopen(s.c_str(),"r",stdin);
s.pop_back();
s.pop_back();
s.pop_back();
s+=".out";
freopen(s.c_str(),"w",stdout);
}
void IOS(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
map<string,int>xs;
string s;
int x;
string ji(string s,int st,int en){
string g="";
for(int i=st;i<=en;i++){
g.push_back(s[i]);
}
return g;
}
string calc(string s){
string ss="";
int ans=0,now=0;
for(int i=0;i<=s.size()-1;i++){
if(i!=s.size()-1&&'A'<=s[i]&&s[i]<='Z'&&'a'<=s[i+1]&&s[i+1]<='z'){
ans+=now;
ss=s[i];
ss+=s[i+1];
now=xs[ss];
#ifdef D
cout<<"A-Z,a-z:"<<i<<" "<<ans<<" "<<now<<" "<<ss<<" "<<xs[ss]<<"\n";
#endif
i++;
if(xs[ss]==0){
return "UNKNOWN";
}
continue;
}
if('A'<=s[i]&&s[i]<='Z'){
ans+=now;
ss=s[i];
now=xs[ss];
#ifdef D
cout<<"A-Z:"<<i<<" "<<ans<<" "<<now<<" "<<ss<<" "<<xs[ss]<<"\n";
#endif
if(xs[ss]==0){
return "UNKNOWN";
}
continue;
}
if('1'<=s[i]&&s[i]<='9'&&'1'<=s[i+1]&&s[i+1]<='9'){
now*=(s[i]-'0')*10+(s[i+1]-'0');
i++;
#ifdef D
cout<<"0-9,0-9:"<<now<<"\n";
#endif
continue;
}
if('1'<=s[i]&&s[i]<='9'){
now*=(s[i]-'0');
#ifdef D
cout<<"0-9:"<<now<<"\n";
#endif
continue;
}
if(s[i]=='('){
ans+=now;
int j=i+1;
int t=1;
while(t){
i++;
if(s[i]=='('){
t++;
}
if(s[i]==')'){
t--;
}
}
#ifdef D
cout<<"(:"<<j<<" "<<i-1<<" "<<ji(s,j,i-1)<<"\n";
#endif
string k=calc(ji(s,j,i-1));
if(k=="UNKNOWN"){
return "UNKNOWN";
}
now=atoll(k.c_str());
continue;
}
}
ans+=now;
return to_string(ans);
}
signed main(){
IOS();
cin>>s;
while(s!="END_OF_FIRST_PART"){
cin>>x;
xs[s]=x;
cin>>s;
}
cin>>s;
while(s!="0"){
cout<<calc(s)<<"\n";
cin>>s;
}
return 0;
}