#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
map<string, int> mp;
P parse(int n, string s, int st) {
  int res = 0, tmp = 0, mul = 0;
  string now = "";
  for(int i=st;i<n;++i) {
    if(s[i] == '(') {
      P p = parse(n, s, i+1);
      if(p == make_pair(-1, -1)) {
        return {-1, -1};
      }
      tmp += p.first;
      i = p.second;
    } else if(s[i] == ')') {
      return {res, i};
    } else if('0' <= s[i] && s[i] <= '9') {
      mul = mul * 10 + (s[i] - '0');
      if(i == n-1 || s[i+1] < '0' || '9' < s[i+1]) {
        res += tmp * mul;
        tmp = 0;
        mul = 0;
      }
    } else {
      now.push_back(s[i]);
      if(!('a' <= s[i+1] && s[i+1] <= 'z')) {
        if(mp.find(now) == mp.end()) {
          return {-1, -1};
        } else {
          if(i == n-1 || s[i+1] < '0' || '9' < s[i+1]) res += mp[now];
          else tmp += mp[now];
          now = "";
        }
      }
    }
  }
  return {res, n};
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    string s; cin >> s;
    if(s == "END_OF_FIRST_PART") break;
    int tmp; cin >> tmp;
    mp[s] = tmp;
  }
  while(1) {
    string s; cin >> s;
    if(s == "0") break;
    P p = parse((int)(s.size()), s, 0);
    cout << (p == make_pair(-1, -1) ? "UNKNOWN" : to_string(p.first)) << endl;
  }
}

