#include<bits/stdc++.h>
using namespace std;
int n;
string s[10000];
string s1;
string c="END_OF_FIRST_PART";
map<string,int> m;
string sub(string x){
int n=0,flag=0;
int z[100];
for(int i=0;i<x.size();i++){
if(x[i]>='0' && x[i]<='9' || x[i]=='(')continue;
if(x[i+1]>='A' && x[i+1]<='Z' || x[i+1]=='(' || x[i+1]==')' || i+1>=x.size()){
string s1=x.substr(0,i+1);
string s2=x.substr(i+1,x.size()-i);
x=s1+'1'+s2;
}
}
return x;
}
void f(string x){
int l=0,ans=0,w=0;
int flag=0;
string fenzi;
while(l<x.size()){
if(x[l]=='(')flag+=1,l++;
if(x[l]==')'){
flag-=1,l++;
n=x[l]-'0';
if(x[l+1]>='0' && x[l+1]<='9'){
l++;
n=n*10+x[l]-'0';
}
w*=n;
if(!flag){
ans+=w;
w=0;
}
l++;
}
if(x[l]>='A' && x[l]<='Z'){
fenzi=x[l];
if(x[l+1]>='a' && x[l+1]<='z'){
l++;
fenzi+=x[l];
}
l++;
n=x[l]-'0';
if(x[l+1]>='0' && x[l+1]<='9'){
l++;
n=n*10+x[l]-'0';
}
if(m[fenzi]){
if(flag)w+=m[fenzi]*n,l++;
else ans+=m[fenzi]*n,l++;
}else{
cout<<"UNKNOWN"<<endl;
return;
}
}
}
cout<<ans<<endl;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>s[++n] && s[n]!=c)cin>>m[s[n]];
while(cin>>s1 && s1!="0"){
s1=sub(s1);
f(s1);
}
return 0;
}