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

int idx;
string s;
map<string,int> atom;

int Number(){
	string res = "";
	while(isdigit(s[idx])){
		res += s[idx++];
	}
	return atoi(&res[0]);
}

int Atom(){
	string res = "";
	res += s[idx++];
	if(islower(s[idx])){
		res += s[idx++];
	}
	return atom.find(res) != atom.end() ? atom[res] : -1;
}

int Molecule(){
	int res = 0;

	while(idx < s.length() && s[idx] != ')'){
		if(s[idx] == '('){
			idx++;
			int tmp = Molecule();
			if(tmp == -1) return -1;
			idx++;
			res += tmp * Number();
		}
		else{
			int tmp = Atom();
			if(tmp == -1) return -1;
			if(isdigit(s[idx])){
				res += tmp * Number();
			}
			else{
				res += tmp;
			}
		}
	}

	return res;
}

int main(void){
	while(cin>>s,s!="END_OF_FIRST_PART"){
		int w;
		cin>>w;
		atom[s] = w;
	}

	while(cin>>s,s!="0"){
		idx = 0;
		int rst = Molecule();
		if(rst == -1)
			cout<<"UNKNOWN\n";
		else
			cout<<rst<<endl;
	}

	return 0;
}