When Isaac travels abroad — say, to France — he exchanges his US dollars for the local currency (French francs). An exchange rate is a real number: multiplying the number of dollars by the rate gives the number of francs. For example, if the exchange rate from US dollars to French francs is 4.81724, then 10 dollars becomes 48.1724 francs.
You can only hold hundredths of a unit of currency, so every exchanged amount is rounded to the nearest hundredth, with .005 always rounded up to .01. Every exchange between any two currencies is rounded this way.
Some of Isaac's trips take him through several countries, exchanging the money of one foreign country directly for that of the next. When he finally comes home he exchanges whatever is left back into US dollars. Isaac wants to know how much of his unspent money is lost (or gained) to these exchange rates. Given the exchange rates and a sequence of trips, compute how many US dollars Isaac ends up with after each trip. Every trip starts and ends in US dollars.
There are 5 countries, numbered 1 through 5; country 1 is the United States.
The first 5 lines describe the exchange-rate matrix. Line i contains 5 real numbers; the j-th number is the exchange rate from the currency of country i to the currency of country j. The rate from a country to itself is always 1.
Each of the following lines describes one trip in the form:
n c1 c2 … cn m
where 1≤n≤10 and each c1,…,cn is an integer from 2 to 5 giving, in order, the countries Isaac visits. The trip is therefore 1→c1→c2→⋯→cn→1. The real number m is the amount of US dollars Isaac starts the trip with.
A line whose first number is n=0 marks the end of the input; it contains no further numbers.
For each trip, print one line with the number of US dollars Isaac has when he returns home, rounded to the nearest cent. Print the value in the usual way, with the cents shown after the decimal point and exactly two digits after it. If the amount is less than one dollar, print a 0 in the dollars place (for example, 0.99).