#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

map<string, int> table;
string mole;
int idx;

int getNum()
{
  string ret = "";
  while(idx < mole.size() && isdigit(mole[idx])) ret += mole[idx++];
  return stoi(ret);
}

int getAtom()
{
  string atom = "";
  atom += mole[idx++];
  while(idx < mole.size() && islower(mole[idx])) atom += mole[idx++];
  if(table.count(atom)) return table[atom];
  return -1;
}

int solve()
{
  int ret = 0;
  if(idx == mole.size()) return 0;
  if(mole[idx] == ')') return 0;
  if(mole[idx] == '(') {
    idx++; // skip '('
    ret = solve();
    if(ret == -1) return -1;
    idx++; // skip ')'
    if(isdigit(mole[idx])) ret *= getNum();
  } else {
    ret = getAtom();
    if(ret == -1) return -1;
    if(isdigit(mole[idx])) ret *= getNum();
  }
  int tmp = solve();
  if(tmp == -1) return -1;
  return ret + tmp;
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string atom;
  int weight;
  while(cin >> atom, atom != "END_OF_FIRST_PART") {
    cin >> weight;
    table[atom] = weight;
  }

  while(cin >> mole, mole != "0") {
    idx = 0;
    int ans = solve();
    if(ans == -1) cout << "UNKNOWN" << endl;
    else cout << ans << endl;
  }

  return 0;
}