#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<string>
#include<map>
#include<vector>
using namespace std;
struct I{int x;I():x(-1){}operator int(){return x;}void operator=(int r){x=r;}};
int main()
{
	char a[128],*p,*q;
	int w,x,i;
	map<string,I>m;vector<int>v,s;while(scanf("%s",a),strcmp(a,"END_OF_FIRST_PART"))scanf("%d",&w),m[a]=v.size(),v.push_back(w);
	while(scanf("%s",a),a[0]-'0')
	{
		s.clear();
		s.push_back(0);
		for(p=a;*p;)
		{
			if(isupper(*p))
			{
				if(islower(p[1]))i=m[string(p,2)],p+=2;
				else i=m[string(p,1)],++p;
				if(i<0){puts("UNKNOWN");goto E;}
				if(isdigit(*p))x=strtol(p,&q,10),p=q;
				else x=1;
				s.back()+=x*v[i];
			}
			else if(*p=='(')s.push_back(0),++p;
			else if(*p==')')
			{
				++p;
				w=s.back(),s.pop_back();
				x=strtol(p,&q,10);
				if(p==q)x=1;
				p=q;
				s.back()+=w*x;
			}
		}
		printf("%d\n",s[0]);
E:;
	}
	return 0;
}