#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

map<string,int> mm;

int perseMolecule(const string &str){
  if(str.size() == 0) return 0;
  //cout << "molecule: " << str << endl;
  if(str[0] == '('){
    int d = 1;
    int pos = 1;
    while(true){
      if(str[pos] == '(') d++;
      if(str[pos] == ')') d--;
      if(d == 0) break;
      pos++;
    }
    string str1 = str.substr(1,pos-1);
    int ret    = perseMolecule(str1);
    if(pos + 1 != str.size()){
      string str2 = str.substr(pos+1);
      if(isdigit(str2[0])){
        int p = 1;
        int t = str2[0] - '0';
        if(str2.size() != 1 && isdigit(str2[1])){
          t = t*10 + str2[1] - '0';
          p = 2;
        }
        ret *= t;
        str2 = str2.substr(p);
      }
      ret += perseMolecule(str2);
    }
    return ret;
  }else{
    string str1,str2;
    if(str.size() != 1 && islower(str[1])){
      str1 = str.substr(0,2);
      str2 = str.substr(2);
    }else{
      str1 = str.substr(0,1);
      str2 = str.substr(1);
    }
    if(mm.count(str1) == 0) throw "error";
    //cout << str1 << ": " << mm[str1] << endl;
    int ret = mm[str1];
    if(str2.size()!=0 && isdigit(str2[0])){
      int p = 1;
      int t = str2[0] - '0';
      if(str2.size() != 1 && isdigit(str2[1])){
        t = t*10 + str2[1] - '0';
        p = 2;
      }
      ret *= t;
      str2 = str2.substr(p);
    }
    ret += perseMolecule(str2);
    return ret;
  }
}

int main(){
  string str;
  while(cin >> str, str != "END_OF_FIRST_PART"){
    int n; cin >> n;
    mm[str] = n;
  }

  while(cin >> str, str != "0"){
    try{
      cout << perseMolecule(str) << endl;
    }catch(...){
      cout << "UNKNOWN" << endl;
    }
  }
  return 0;
}