#include <iostream>
#include <algorithm>
#include <map>
#define S str[ima]
using namespace std;
map <string,int> mp;
int ima;
string str;

int check(char ch){
  if('A'<=ch && ch<='Z') return 1;
  if('a'<=ch && ch<='z') return 2;
  if('0'<=ch && ch<='9') return 3;
  if(ch == '(') return 4;
  return 5;
}

int formula(){
  int res=0;
  while(S != '\0'){
    int num=0;
    if(check(S) == 1) {
      string a="";
      a+=S;
      ima++;
      while(check(S)==2) a+=S,ima++;
      if(mp.count(a) != 1) return -1;
      while(check(S)==3) num*=10,num+=S-'0',ima++;
      res += (mp[a]*max(1,num));
    }
    else if(check(S)==4){
      ima++;
      int b =formula();
      if(b == -1) return -1;
      ima++;
      while(check(S)==3) num*=10,num+=S-'0',ima++;
      res += (b*max(1,num));
    }
    else break;
  }
  return res;
}

int main(){
  while(1){
    int n;
    cin >> str;
    if(str == "END_OF_FIRST_PART")break;
    cin >> n;
    mp[str] = n;
  }
  while(1){
    cin >> str;
    if(str=="0") break;
    ima = 0;
    int res = formula();
    if(res==-1 || res==0) cout <<"UNKNOWN"<<endl;
    else cout << res <<endl;
  }
  return 0;
}