#include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <map>
#include <vector>
#include <string>
#include <stdexcept>

int parse(const std::string& s, size_t& i,
          const std::map<std::string, int>& mole) {

  int res = 0;
  while (i < s.length() && s[i] != ')') {
    int cur = 0;
    if (s[i] == '(') {
      cur = parse(s, ++i, mole);
      assert(s[i] == ')');
      ++i;
    } else if (isupper(s[i])) {
      cur = mole.at(s.substr(i, 2));
      i += 2;
    } else {
      fprintf(stderr, "%s[%zu]\n", s.c_str(), i);
      assert(false);
    }
    if (isdigit(s[i])) {
      int k = s[i]-'0';
      while (++i < s.length() && isdigit(s[i]))
        k = k*10+s[i]-'0';
      cur *= k;
    }
    res += cur;
  }
  return res;
}

int main() {
  std::map<std::string, int> mole;
  while (true) {
    char buf[4];
    int d;
    scanf("%s %d", buf, &d);
    std::string s = buf;
    if (s.length() == 1) s += '.';
    if (s == "END_OF_FIRST_PART") break;
    mole[s] = d;
  }

  while (true) {
    char buf[128];
    scanf("%s", buf);
    std::string s = buf;
    if (s == "0") break;
    std::string t;
    for (size_t i=0; i<s.length(); ++i) {
      t += s[i];
      if (!isupper(s[i])) continue;
      if (!islower(s[i+1]))
        t += '.';
    }
    try {
      size_t i = 0;
      printf("%d\n", parse(t, i, mole));
    } catch (std::logic_error&) {
      puts("UNKNOWN");
    }
  }
}    

