#include <bits/stdc++.h>
using namespace std;

bool f;

int expr(string, int&);
int term(string, int&);
int factor(string, int&);
int number(string, int&);


int expr(string s, int& p) {
  int res = term(s, p);
  while ( p < (int)s.size() ) {
    if ( s[p] == '+' ) {
      p++;
      res += term(s, p);
      continue;
    }
    break;
  }
  return res;
}

int term(string s, int& p) {
  int res = factor(s, p);
  while ( p < (int)s.size() ) {
    if ( s[p] == '*' ) {
      p++;
      res *= factor(s, p);
      continue;
    }
    break;
  }
  return res;
}

int factor(string s, int& p) {
  int res;
  if ( s[p] == '(' ) {
    p++;
    res = expr(s, p);
    p++;
  } else {
    res = number(s, p);
  }
  return res;
}

int number(string s, int& p) {
  int res = 0;
  while ( p < (int)s.size() && isdigit(s[p]) ) {
    res = res*10+s[p++]-'0';    
  }
  return res;  
}

int main() {
  map<string, int> wei;
  {
    string s;
    while ( cin >> s, s != "END_OF_FIRST_PART" ) {
      int a;
      cin >> a;
      wei[s] = a;      
    }
  }

  string s;
  while ( cin >> s, s != "0" ) {
    // cout << s << endl;
    string t = "";
    int l = s.size();
    bool flag = true;
    for ( int i = 0; i < l; i++ ) {
      if ( s[i] == '(' || s[i] == ')' ) {
	t.push_back(s[i]);
	if ( s[i] == ')' && i < l-1 ) {	  
	  if ( isdigit(s[i+1]) ) t.push_back('*');
	  else if ( s[i+1] != ')' ) {
	    t.push_back('+');
	  }
	}
      }
      else if ( isdigit(s[i]) ) {
	string add = "";	
	while ( 1 ) {
	  if ( i == l || !isdigit(s[i]) ) {	    
	    i--;
	    break;
	  }
	  add += s[i];
	  i++;
	}
	t += add;	
	if ( i < l-1 ) {	  
	  if ( isdigit(s[i+1]) ) t.push_back('*');
	  else if ( s[i+1] != ')' ) {
	    t.push_back('+');
	  }
	}
      } else {
	string a = ""; a += s[i];	
	if ( i < l-1 && 'a' <= s[i+1] && s[i+1] <= 'z' ) {
	  a += s[i+1];
	  i++;	  
	}
	if ( wei.count(a) == 0 ) {
	  flag = false;	  
	  break;	  
	} else {
	  t += to_string(wei[a]);	  
	}
	if ( i < l-1 ) {	  
	  if ( isdigit(s[i+1]) ) t.push_back('*');
	  else if ( s[i+1] != ')' ) {
	    t.push_back('+');
	  }
	}
      }
    }
    if ( !flag ) {
      cout << "UNKNOWN" << endl;
    }
    else {
      int p = 0;
      cout << expr(t, p) << endl;
    }
  }
}

