Balanced Chemical Equations#6846

Balanced Chemical Equations

1 second
64 megabytes
Hard

One cumbersome problem in chemistry is the task of making the number of atoms balanced in a chemical equation. Our problem is concerned with this.

Chemists obey these rules when they present chemical equations:

  1. Each element name is abbreviated by at most two letters. The first letter is always in upper-case and the second letter if exists, is a lower-case letter (e.g. Calcium is represented by Ca, Oxygen by O, and Chlorine by Cl).

  2. Each molecule is composed of a number of atoms. To represent a molecule, we concatenate the abbreviated names of its composite atoms. For example, NaCl represents Sodium Chloride. Each atom name may be followed by a frequency number. For example, Calcium Chloride CaCl2 consists of one atom of Calcium and two atoms of Chlorine. If the frequency is not given, it is assumed to be (so HCl is equivalent to H1Cl1). For the sake of simplicity, you may assume that the frequency of an occurrence of an atom is at most (so we do not have C11H22O11 in the problem input). Note that there may be several occurrences of the same atom in the molecule formula, like H atom in CH3COOH.

  3. In ordinary chemical reactions, a number of molecules combine and result in a number of other molecules. For example a well known sample of neutralization is:

2HCl + CaO2H2 → CaCl2 + 2H2O

This means two molecules of chlorohydric acid (HCl) with one molecule of Calcium Hydroxide results in one molecule of Calcium Chloride (CaCl2) and two molecules of water.

  1. In every chemical reaction, the total number of each atom in the right side of the equation equals the total number of that atom in the left side (that is why it is called an equation!).

Your task is to write a program to find appropriate coefficients x_1, x_2, \ldots, x_M and y_1, y_2, \ldots, y_N to balance an (imbalanced) equation like

A_1+A_2+A_3+ \ldots +A_M \rightarrow B_1+B_2+B_3+ \ldots +B_N

in the following way:

x_1 A_1+ x_2 A_2+ x_3 A_3+ \ldots + x_M A_M \rightarrow y_1 B_1+ y_2 B_2+ y_3 B_3+ \ldots + y_N B_N

Input

The first line contains an integer (1 \le t \le 10), the number of test cases. Each test case consists of a single line containing an expression like:

A_1+A_2+A_3+ \ldots +A_M= B_1+B_2+B_3+ \ldots +B_N

Each A_i or B_i is a formula of a molecule according to the rules given in items and .

The input equations are given in a way that if they can be balanced, x_i and y_i coefficients can be in the range of to . There are less than molecules and there are less than different types of atoms, in a given equation. Additionally, you may assume molecules contain no more than different kinds of atoms. You may also assume that there is no blank character in the input file, and the maximum length of the input lines is characters.

Output

The output will be one line per test case containing the list of required coefficients, separated by blank characters, in the following order:

x_1 \; x_2 \; \ldots \; x_M \; y_1 \; y_2 \; \ldots \; y_N

The coefficients should be integers in the range of to . Obviously, there may be more than one answer for a test case. In such situations, print the answer which minimizes the number:

(This is an (M+N)-digit decimal number whose digits are x_i and y_i coefficients.) If the equation is impossible to balance, the output line should be IMPOSSIBLE.

Examples

Example 1

Loading
Loading