#P2695. Molecular Weight Calculator

    ID: 1592 远端评测题 20000ms 32MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>The 4th Baidu Cup Central China Invitational Programming Contest

Molecular Weight Calculator

Problem Description

Even though not good at memorizing, Alex is fond of Chemistry. When solving problems, he always needs to calculate the
molecular weight and quantity of each kind of elements. Lazy Alex hates that and turns to you for help.

Your task is:
Given a chemical formula, calculate its molecular weight and quantity of each kind of elements.

The atom is represented by either a single capital letter or a capital letter and a lower case letter. E.g. C, Ca, H, O. Formulas
may contain brackets: '(',')', which is always followed by the quantity of that atomic mission. The number after an atom or a
bracket represents the quantity of that atomic mission. E.g. CH4, CaCO3, Fe(SCN)3. Brackets may be nested.

Input

The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.

Each test case is on several lines. The first line contains an integer, N, indicating the quantity of known elements.
Each of the following n lines is consisting of a element description, start with a element name and followed by the weight
W (floating point number), separated by a space.
The (n+1)-th line consists an integer M, indicating the quantity of formulas.
Each of the following M lines is consisting of a formula.

[Technical Specification]
1.T is an integer, and T <=10.
2.N is an integer, and 1 <= N <= 200.
3.W is a floating point number with two fractional digits, and 1 <= W <= 400.
4.M is an integer, and 1 <= M <= 100.
5.Numbers in the formula is always in the range of [2,9] inclusive.
6.The formula?s length does not exceed 500.
7.There's no blank line between two test cases.

Output

For each formula in each test case, print a single line as the format below.
Format:
Weight Element[quantity] Element[quantity] Element[quantity] ...
e.g. 44.00 C[1] O[2]
The element is ordered as they were in the input. Display all the elements except for those not in the formula. The weight is
rounded to two fractional digits. Notice that the answer may be very large.
There's no whitespace at the end of each line.

1 7 H 1.00 C 12.00 O 16.00 N 14.00 S 32.00 Ca 40.00 Fe 56.00 4 CO2 CH4 CaCO3 Fe(SCN)3
44.00 C[1] O[2] 16.00 H[4] C[1] 100.00 C[1] O[3] Ca[1] 230.00 C[3] N[3] S[3] Fe[1]