#include <iostream>
#include <map>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;

string S;
int W;
map<string, int> P;
int i;

int number() {
  int n = S[i++] - '0';
  if('0' <= S[i] && S[i] <= '9') n = n * 10 + (S[i++] - '0');
  return n;
}

int atom() {
  string name = "";
  name += S[i++];
  if('a' <= S[i] && S[i] <= 'z') name += S[i++];
  return P[name] > 0 ? P[name] : -1;
}

int molecular() {
  int weight = 0, w, n;
  while(S[i] != ')' && i < S.length()) {
    if(S[i] == '(') {
      i++;
      w = molecular();
      i++;
      n = number();
    } else {
      if((w = atom()) == -1) return -1;
      n = ('0' <= S[i] && S[i] <= '9' ? number() : 1);
    }
    weight += w * n;
  }
  return weight;
}

int main(void) {
  while(cin >> S, S != "END_OF_FIRST_PART") {
    cin >> W;
    P[S] = W;
  }

  while(cin >> S, S != "0") {
    i = 0;
    int ans = molecular();
    if(ans > 0) cout << ans << endl;
    else cout << "UNKNOWN" << endl;
  }

  return 0;
}