atoms = []
loop {
    atom, weight = gets.split
    break if atom =~ /^END/
    atoms << [atom, ?+ + weight]
}
atoms.sort_by!{|atom, _| -atom.size}

loop {
    s = gets.chomp
    break if s == ?0
    s.gsub!(/(\D)(\d)/){|x| $1 + "*" + $2}
    s.gsub!("(", "+(")
    atoms.each{|atom, weight|
        [atom, weight]
        s.gsub!(atom, weight)
    }
    if s =~ /[a-zA-Z]/
        puts 'UNKNOWN'
        next
    end

    p eval s
}