Mileage Bank

No attempts yetTime limit1sMemory limit128 MB

Problem

ACM (Airline of Charming Merlion) runs a mileage program for frequent flyers. Each time you complete a flight with ACM, you earn ACMPerk miles in your ACM Mileage Bank based on the distance you actually fly, and you can later redeem accumulated ACMPerk miles for free ACM tickets.

How many ACMPerk miles a single flight earns depends on the cabin class:

Cabin classClass codeACMPerk miles earned
First ClassFactual mileage + 100% bonus
Business ClassBactual mileage + 50% bonus
Economy ClassY500 miles for an actual distance of 1-500 miles; the actual mileage for a distance over 500 miles

Your ACMPerk miles for a flight are the sum of two parts: the actual flight mileage (with a minimum of 500 miles per Economy-Class flight) and, for Business and First Class, the mileage bonus. The bonus is credited to a precision of one mile, rounding a half mile up.

For instance, the flight from Beijing to Tokyo has an actual mileage of 1329 miles, so it earns 1329 ACMPerk miles in Economy (Y), 1994 in Business (B), and 2658 in First (F). The flight from Shanghai to Wuhan has an actual mileage of 433 miles, so it earns 500 ACMPerk miles in Economy (Y) and 650 in Business (B).

Write a program that computes, for each test case, the total ACMPerk miles earned across all of its flights.

Input

The input consists of several test cases. Each test case contains one or more flight records, one per line, in the format:

Origin Destination ActualMiles ClassCode

Origin and Destination are the departure and arrival city names, ActualMiles is the actual flight distance as a non-negative integer, and ClassCode is one of F, B, or Y. Each test case ends with a line containing a single 0. A line containing a single # marks the end of the input.

Output

For each test case, output on a single line the total ACMPerk miles earned across all flight records in that test case.