#include <iostream>
#include <map>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl

map<string,int> f;
string s;
int i;

int molecules();
int molecule();
int atom();
int number();

int molecules()
{
	//cout<<"molecules "<<i<<endl;
	int x=0;
	while(i<s.size() && s[i]!=')')
		x+=molecule();
	return x;
}

int molecule()
{
	//cout<<"molecule "<<i<<endl;
	if(i==s.size())
		return 0;
	if(s[i]=='('){
		i++;
		int m=molecules();
		i++;
		int n=number();
		return m*n;
	}
	int x=atom();
	if(i<s.size() && isdigit(s[i]))
		x*=number();
	return x;
}

int atom()
{
	//cout<<"atom "<<i<<endl;
	string a(1,s[i++]);
	if(i<s.size() && islower(s[i]))
		a+=s[i++];
	if(!f.count(a))
		throw "UNKNOWN";
	return f[a];
}

int number()
{
	//cout<<"number "<<i<<endl;
	int n=0;
	while(i<s.size() && isdigit(s[i]))
		n=n*10+s[i++]-'0';
	return n;
}

int main()
{
	while(cin>>s && s!="END_OF_FIRST_PART"){
		int w; cin>>w;
		f[s]=w;
	}
	while(cin>>s && s!="0"){
		i=0;
		try{
			cout<<molecules()<<endl;
		}
		catch(const char* message){
			cout<<message<<endl;
		}
	}
}