Show Me the Money
Frank Marks works at the Business Oce of a large company. His company has customers all over the world and must deal with many dierent currencies. Employees often come to the Business Oce with requisitions for certain amounts of money, such as 100 American dollars or 452 Euros. If Frank has the cash on hand, he gives the employee exactly what they need; if he does not have enough of the particular currency requested, he substitutes it with another one. This is sometimes dicult because he may have many dierent currencies to choose from and would like to pick the one which allows him to get as close to the original requisition as possible without going under (he must provide at least the value requested). For example, suppose Frank has six dierence currencies - A, B, C, D, E and F - and he is aware of the following exchange rates:
23 A = 17 B
16 C = 29 E
5 B = 14 E
1 D = 7 F
If a requisition for 100 A comes in but Frank has less than 100 A available, he can substitute with either 74 B(equivalent to about 100.12 A), 115 C (equivalent to about 100.72 A) or 207 E (equivalent to about 100.02 A). Thus, the best approximation available to him is 207 E. Note that Frank does not have enough information to find a relationship between currencies A and D or currencies A and F. Also, Frank only has at most 100000 units of any one currency in stock, so he could not satisfy a request for 64000 A using E currency and would need to use 73078 Cinstead.
Determining the ideal substitute currency to use when he has completely run out of the requested currency is time consuming, so Frank would like a program to do the calculations for him.
Input
Each test case begins with a positive integer n indicating the number of exchange rates. The next n lines will be of the form
val_1 name_1 = val_2 name_2
where name_1 and name_2 are the names of two distinct currencies, and val_1 and val_2 are positive integers ≤ 30specifying the ratio between the two currencies. There will be no more than 8 distinct currency names, and any two currencies will be paired together at most once. Currency names will consist of up to ten alphabetic characters. There will be no inconsistencies in the input (such as 1A = 2B, 1B = 2C and 1C = 2A). Following these n lines will be a single line of the form
val name
which speci es the amount (a positive integer not exceeding 100000) and the name of the currency requested. A line containing 0 will follow the last test case.
Output
For each test case, print the case number and the closest approximation without going under the requested value assuming that Frank does not have any of the requested currency available but is fully in stock of all other currencies. There will be a unique answer for each problem instance.