#include <iostream>
#include <string>
#include <map>
#include <cctype>
#include <cstdlib>
using namespace std;

map<string, int> at;

bool isnumber(char c) {
  return c >= '0' && c <= '9';
}

string get(string str) {
  string s = "";
  s += str[0];
  if(str[0] == '(' || str[0] == ')') return s;
  if(str.length() == 1) return s;
  if(isalpha(str[0])) {
    if(islower(str[1])) return s + str[1];
    else                return s;
  } else {
    for(int i = 1; i < str.length() && isnumber(str[i]); i++) {
      s += str[i];
    }
    return s;
  }
}

int pos;
string sss;
int solve() {
  int w = 0;
  int cnt = 0;
  while(pos < sss.length()) {
    string s = get(sss.substr(pos, sss.length()-pos));
    pos += s.length();
    if(s == "(") {
      cnt += w;
      w = solve();
      if(w == 0) return 0;
    } else if(s == ")") {
      cnt += w;
      return cnt;
    } else if(isalpha(s[0])) {
      cnt += w;
      w = at[s];
      if(w == 0) return 0;
    } else {
      cnt += w*atoi(s.c_str());
      w = 0;
    }
  }
  cnt += w;
  return cnt;
}

int main() {
  string str;
  while(cin >> str) {
    if(str == "END_OF_FIRST_PART") break;
    cin >> at[str];
  }
  while((cin >> sss) && sss != "0") {
    pos = 0;
    int ans = solve();
    if(ans == 0) cout << "UNKNOWN" << endl;
    else         cout << ans << endl;
  }
}