{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"Your mission in this problem is to write a computer program that manipulates molecular formulae in virtual chemistry. As in real chemistry, each molecular formula represents a molecule consisting of one or more atoms. However, it may not have chemical reality. \r\u003cbr\u003eThe following are the definitions of atomic symbols and molecular formulae you should consider.\r\u003cbr\u003e\r\u003cbr\u003eAn atom in a molecule is represented by an atomic symbol, which is either a single capital letter or a capital letter followed by a small letter. For instance H and He are atomic symbols.\r\u003cbr\u003e\r\u003cbr\u003eA molecular formula is a non-empty sequence of atomic symbols. For instance, HHHeHHHe is a molecular formula, and represents a molecule consisting of four H\u0027s and two He\u0027s.\r\u003cbr\u003e\r\u003cbr\u003eFor convenience, a repetition of the same sub-formula X...X, where n is an integer between 2 and 99 inclusive, can be abbreviated to (X)n. Parentheses can be omitted if X is an atomic symbol. For instance, HHHeHHHe is also written as H2HeH2He, (HHHe)2, (H2He)2, or even ((H)2He)2.\r\u003cbr\u003e\r\u003cbr\u003eThe set of all molecular formulae can be viewed as a formal language. Summarizing the above description, the syntax of molecular formulae is defined as follows.\r\u003cbr\u003e\r\u003cbr\u003eMolecule -\u0026gt; Atom | Atom Number | ( Molecule ) Number | Molecule Molecule\r\u003cbr\u003eAtom -\u0026gt; CapitalLetter | CapitalLetter SmallLetter\r\u003cbr\u003eNumber -\u0026gt; 2 | 3 | 4 | . . . | 97 | 98 | 99\r\u003cbr\u003eCapitalLetter -\u0026gt; A | B | C | . . . | X | Y | Z\r\u003cbr\u003eSmallLetter -\u0026gt; a | b | c | . . . | x | y | z\r\u003cbr\u003e\r\u003cbr\u003eEach atom in our virtual chemistry has its own atomic weight. Given the weights of atoms, your program should calculate the weight of a molecule represented by a molecular formula. The molecular weight is defined by the sum of the weights of the constituent atoms. For instance, assuming that the atomic weights of the atoms whose symbols are H and He are 1 and 4, respectively, the total weight of a molecule represented by (H2He)2 is 12.\r\u003cbr\u003e\r\u003cbr\u003e"}},{"title":"Input","value":{"format":"HTML","content":"The input consists of two parts. The first part, the Atomic Table, is composed of a number of lines, each line including an atomic symbol, one or more spaces, and its atomic weight which is a positive integer no more than 1000. No two lines include the same atomic symbol.\r\u003cbr\u003e\r\u003cbr\u003eThe first part ends with a line containing only the string END_OF_FIRST_PART.\r\u003cbr\u003e\r\u003cbr\u003eThe second part of the input is a sequence of lines. Each line is a molecular formula, not exceeding 80 characters, and contains no spaces. A molecule contains at most 10^5 atoms. Some atomic symbols in a molecular formula may not appear in the Atomic Table.\r\u003cbr\u003e\r\u003cbr\u003eThe sequence is followed by a line containing a single zero, indicating the end of the input.\r\u003cbr\u003e"}},{"title":"Output","value":{"format":"HTML","content":"The output is a sequence of lines, one for each line of the second part of the input. Each line contains either an integer, the molecular weight for a given molecular formula in the corresponding input line if all its atomic symbols appear in the Atomic Table, or UNKNOWN otherwise. No extra characters are allowed."}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003eH 1\r\nHe 4\r\nC 12\r\nO 16\r\nF 19\r\nNe 20\r\nCu 64\r\nCc 333\r\nEND_OF_FIRST_PART\r\nH2C\r\n(MgF)2As\r\nCu(OH)2\r\nH((CO)2F)99\r\n0\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e14\r\nUNKNOWN\r\n98\r\n7426\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}