#include <iostream>
#include <cassert>
#include <string>
#include <map>
using namespace std;

map<string,int> list;
string S;
size_t cur=0;
bool flag=true;

int digit();
int number();
string letter();
int atom();
int molecule();

int digit(){
	assert(isdigit(S[cur])); 
	int n = S[cur] - '0'; 
	cur++; 
	return n;
}

int number(){
	int n=digit();
	if(cur <S.size() && isdigit(S[cur]))
		n=n*10+digit();
	return n;
}

string letter(){
	string n;
	n=S[cur];
	cur++;
	return n;
}

int atom(){
	string a = letter();
	if(cur <S.size() &&islower(S[cur]))
		a+=letter();
	int at=0;
	if(list.find(a) == list.end())
		flag=false;
	else
		at=list[a];
	return at;
}


int molecule(){
	int mol=0;
	if(S[cur]=='('){
		cur++;
		int tmp=molecule();
		cur++;
		int num=number();
		mol+=(tmp*num);
	}else if(cur <S.size() &&isupper(S[cur])){
		int tmp=atom();
		int num=1;
		if(cur <S.size() && isdigit(S[cur]))
			num=number();
		mol+=(tmp*num);
	}
	if( cur <S.size()&&S[cur] != ')')
		mol+=molecule();
	return mol;
}


int main(void){
	string sin;
	int iin;
	while(cin >> sin, sin !="END_OF_FIRST_PART"){
		cin >> iin;
		list[sin]=iin;
	}
	while(cin >> S,S != "0"){
		flag=true;
		cur=0;
		int r;
		r=molecule();
		if(flag)
			cout << r << endl;
		else 
			cout << "UNKNOWN" << endl;
	}
return 0;
}