Calculating Taxi Fare

No attempts yetTime limit1sMemory limit128 MB

Problem

Taxi fares are complicated: they depend on how far you travel, the time of day, and the traffic. A taxi drives through a sequence of streets $S_1, S_2, \dots, S_n$ in this exact order. Street $S_i$ is $L_i$ kilometers long, and the taxi keeps a constant speed on it, taking $M_i$ minutes to drive one kilometer of $S_i$.

A passenger boards at the start of some street $S_i$ and gets off at the end of a later street $S_j$ (boarding and alighting always happen at a street's end, never in the middle of a street). The passenger is charged per kilometer:

  • Each of the first 10 kilometers costs 1000 Rials.
  • Each of the next 20 kilometers (kilometers 11 through 30) costs 250 Rials.
  • Each kilometer after the 30th costs 100 Rials.

Two surcharges may then apply:

  • Night surcharge. Consider each kilometer separately. If the taxi spends at least one minute driving that kilometer during the interval from 12:00 AM to 6:00 AM, that kilometer costs 20% more.
  • Traffic surcharge. If the average speed over the whole trip is less than 30 km/h, the fare is increased by 10%.

The night surcharge is added per kilometer first; the 10% traffic surcharge is then applied once to the resulting total.

Input

The input contains several test cases. Each test case has two parts.

The first part lists the streets the taxi drives through, one per line and in travel order:

street-name length min

Here street-name is a unique string of at most 20 letters and digits with no spaces, length is $L_i$ in kilometers (at most 200), and min is $M_i$ in minutes; both are positive integers. Each street is visited exactly once. This part ends with a line containing only a single $ character.

The second part is one line:

source-street dest-street time

source-street and dest-street are the boarding and alighting street names, and time is the boarding time in 24-hour HH:MM format. Both streets belong to the street list, and the destination street does not come before the source street. A line containing only a single # character ends each test case.

The input ends with a line containing two dash characters --.

Output

For each test case, print a single line containing the fare of the passenger's trip.