object Main extends App {

  import scala.io.StdIn._

  implicit val map = readAtoms
  solve
  def solve: Unit = {
    val molecule = readLine.trim
    if (molecule == "0") return
    molecule match {
      case MoleculeParse(m) ⇒ m.calculateWeight match {
        case Some(w) ⇒ println(w)
        case None ⇒ println("UNKNOWN")
      }
      case _ ⇒ ???
    }
    solve
  }
  def readAtoms: String => Option[Int]= {
    var result: List[(String, Int)] = Nil
    var arg = readLine.trim
    while (arg != "END_OF_FIRST_PART"){
      val Array(name, weight) = arg.split(' ').filterNot(_.length == 0)
      result ::= (name, weight.toInt)
      arg = readLine.trim
    }
    result.toMap.mapValues(i⇒Some(i)).withDefault(_ ⇒ None)
  }

  object MoleculeParse {
    def unapply(arg: String): Option[Molecule] = {
      arg.toCharArray.toList match {
        case m Molecules Nil ⇒ Some(m)
        case _ ⇒ None
      }
    }
    object Atom{
      def unapply(arg: List[Char]): Option[(Molecule, List[Char])] = {
        arg match {
          case h::h2::t if h.isUpper && h2.isLower ⇒ Some(Molecule.Atom(s"$h$h2"), t)
          case h::t if h.isUpper ⇒ Some(Molecule.Atom(h.toString), t)
          case _ ⇒ None
        }
      }
    }
    object Number {
      def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
        arg.span(_.isDigit) match {
          case (Nil, _) ⇒ None
          case (n, t) ⇒ Some(n.foldLeft(0){(a, b) ⇒ a * 10 + b.asDigit}, t)
        }
      }
    }
    object Atoms{
      def unapply(arg: List[Char]): Option[(Molecule, List[Char])] = {
        arg match {
          case a Atom t ⇒ t match {
            case n Number r ⇒ Some(Molecule.RepeatMolecule(a, n), r)
            case _ ⇒ Some(a, t)
          }
          case '('::(m Molecules ')'::t) ⇒ t match {
            case n Number r ⇒ Some(Molecule.RepeatMolecule(m, n), r)
            case _ ⇒ Some(m, t)
          }
          case _ ⇒ None
        }
      }
    }
    object Molecules {
      def unapply(arg: List[Char]): Option[(Molecule, List[Char])] = {
        arg match {
          case a Atoms t ⇒ t match {
            case m Molecules r ⇒ Some(Molecule.MoleculeSequence(a, m), r)
            case _ ⇒ Some(a, t)
          }
          case _ ⇒ None
        }
      }
    }
  }
  sealed trait Molecule {
    def calculateWeight(implicit map: String ⇒ Option[Int]): Option[Int]
  }
  object Molecule {
    case class Atom(name: String) extends Molecule {
      override def calculateWeight(implicit map: String ⇒ Option[Int]): Option[Int] = map(name)
    }
    case class RepeatMolecule(molecule: Molecule, repeat: Int) extends Molecule {
      override def calculateWeight(implicit map: String ⇒ Option[Int]): Option[Int] = molecule.calculateWeight.map(_ * repeat)
    }
    case class MoleculeSequence(molecule: Molecule, tail: Molecule) extends Molecule {
      override def calculateWeight(implicit map: String ⇒ Option[Int]): Option[Int] = molecule.calculateWeight.flatMap(l ⇒ tail.calculateWeight.map(r ⇒ l + r))
    }
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}


Measured memory_bytes: 44708