using System.Collections.Generic;
using System;

public class hello
{
    public static Dictionary<string, int> d;
    public static void Main()
    {
        d = new Dictionary<string, int>();
        while (true)
        {
            var a = Console.ReadLine().Trim();
            if (a == "END_OF_FIRST_PART") break;
            var atm = a.Substring(0, 2).Replace(" ", "");
            var mw = a.Substring(2).Replace(" ", "");
            d[atm] = int.Parse(mw);
        }
        while (true)
        {
            var s = Console.ReadLine().Trim();
            if (s == "0") break;
            var x = Rec(s);
            if (x == -1) Console.WriteLine("UNKNOWN");
            else Console.WriteLine(x);
        }
    }
    static int check(string s)
    {
        if (s == "(") return 4;
        if (s == ")") return 5;
        if (char.IsUpper(s[0])) return 2;
        else if (char.IsLower(s[0])) return 3;
        return 1;
    }
    static long Rec(string s)
    {
        var pc = 0;
        var mw = 0L;
        var prec = -1;
        var ps = "";
        int w;
        var st = new Stack<string>();
        var ss = s.Substring(0, 1);
        if (ss == "(") pc++;
        else
        {
            st.Push(ss);
            prec = check(ss);
        }

        var sL = s.Length;
        for (int i = 1; i < sL; i++)
        {
            ss = s.Substring(i, 1);
            w = check(ss);
            if (pc == 0)
            {
                if (w == 1)
                {
                    if (prec == 1) st.Push(st.Pop() + ss);
                    else if (prec == 2)
                    {
                        prec = 1;
                        var tkey = st.Peek();
                        int xx;
                        if (int.TryParse(tkey, out xx)) st.Push(ss);
                        else
                        {
                            st.Pop();
                            if (!d.ContainsKey(tkey)) return -1;
                            var tt = d[tkey].ToString();
                            st.Push(tt);
                            st.Push(ss);
                        }
                    }
                }
                else if (w == 2)
                {
                    if (prec == 1)
                    {
                        mw += int.Parse(st.Pop()) * int.Parse(st.Pop());
                        st.Push(ss);
                        prec = 2;
                    }
                    else if (prec == 2)
                    {
                        var tkey = st.Pop();
                        if (!d.ContainsKey(tkey)) return -1;
                        mw += d[tkey];
                        st.Push(ss);
                    }
                }
                else if (w == 3)
                {
                    var tkey = st.Pop() + ss;
                    if (!d.ContainsKey(tkey)) return -1;
                    st.Push(tkey);
                }
                else
                {
                    pc++; 
                    if (prec == 1)
                    {
                        mw += int.Parse(st.Pop()) * int.Parse(st.Pop());
                    }
                    else if (prec == 2)
                    {
                        var tkey = st.Pop();
                        if (!d.ContainsKey(tkey)) return -1;
                        mw += d[tkey];
                    }
                }
            }
            else if (pc > 0)
            {
                if (w == 4)
                {
                    pc++;
                    ps += ss;
                }
                else if (w == 5)
                {
                    pc--;
                    if (pc == 0)
                    {
                        var res = Rec(ps);
                        ps = "";
                        if (res == -1) return -1;
                        else st.Push(res.ToString());
                        prec = 2;   
                    }
                    else ps += ss;
                }
                else ps += ss;
            }
        }
        if (prec == 1)
        {
            mw += int.Parse(st.Pop()) * int.Parse(st.Pop());
        }
        else if (prec == 2)
        {
            var tkey = st.Pop();
            if (!d.ContainsKey(tkey)) return -1;
            mw += d[tkey];
        }
        return mw;
    }
}


Measured memory_bytes: 18380