Units show up everywhere in science. Physics measures distance (meters, kilometers), weight (kilograms, grams) and many other quantities with units, and computer scientists invented units of capacity (kilobytes, megabytes). Given several units, work out how many times larger each one is than the others.
The relations between units can be written in many ways that all mean the same thing. The metric units can be written as 1km = 1000m, 1m = 100cm, 1cm = 10mm, or as 1km = 100000cm, 1km = 1000000mm, 1m = 1000mm. Both ways lead to the same relation 1km = 1000m = 100000cm = 1000000mm.
In this problem you sort the units from largest to smallest. Among cm, km, m and mm the largest unit is km, because 1km is larger than 1cm, 1m and 1mm. Sorting the rest the same way gives km m cm mm.
Sizes always differ by an integer factor here, so a relation such as 1 inch = 2.54cm never appears. Every relation in the input says how many of a smaller unit fit in a larger unit.
The input holds several test cases. Each test case starts with the number of units N (2≤N≤10). The next line holds N unit names. Each name is at most 5 characters long and is case sensitive. The next N−1 lines each describe the relation between two different units in this order: the name of the larger unit, "=", an integer greater than 1, and the name of the smaller unit. Each line says how many of the smaller unit on the right fit in the larger unit on the left. Every unit appears at least once in those N−1 lines, and the given relations are enough to determine the relation between every pair of units. Any two units differ by an integer factor, and that factor is at most 231−1.
A single 0 follows the last test case.
For each test case print the relation between the units on one line. The units run from left to right, largest first. Set the count of the leftmost unit to 1 and list how many of each remaining unit that equals, joining the terms with " = ". In each term the count comes first and the unit name follows it with no space between them. If the largest unit is km and 1km equals 1000m, the line starts with 1km = 1000m.
If two or more units have the same size, print the one whose name comes first in ASCII order. Uppercase letters come before lowercase letters.