Chemical Code is an integrated development environment designed for chemists. Users can write the molecular formula in this editor, and if they move the mouse over the molecular formula, Chemical Code will display its relative molecular mass.
However, Moca, the developer of Chemical Code, did not implement this feature. Due to the tight schedule, she failed to meet the deadline, so that she just displayed the text "The relative molecular mass of CH3(CH2)2OH is 60.''.

The molecular formula that Chemical Code supports consists of three types of symbols -- chemical elements (

), digits and parentheses. The relative molecular mass of

is

. The relative molecular mass of

is

. The relative molecular mass of

is

. The number written after a chemical element denotes that this element is repeated the corresponding number of times, and the number written after a pair of parentheses denotes where the subformula is also repeated the corresponding number of times. For example,

is expanded as

, and
2)
is expanded as
2)
and

. Notice that
nesting parentheses are allowed and
multiple consecutive digits are not allowed. The relative molecular mass of a molecular formula is the sum of the relative molecular mass of all the chemical elements after expanding this molecular formula. The following is the context-free grammar of the molecular formula, and the start symbol is E.
To simplify this feature, Chemical Code can be formed as a character array of length

, and it will handle the user's input operation. In the beginning, the array is initialized with

null characters. The user will perform

operations to fill the entire array. There are two types of the input operation. The first one is inputting a chemical element (

) or a digit in the specified position. And the second one is inputting a pair of parentheses in two specified positions. After each operation, Chemical Code will erase the null characters and print the relative molecular mass of the current molecular formula, which can be derived in the above context-free grammar. It is guaranteed that
each pair of parentheses input by the second type of operation is matching. For example, it is forbidden to input parentheses in the

-nd and

-th position in the molecular formula (␣C)␣.
Moca is asking for your help. Can you implement this feature for her?