Einbahnstrasse#1335

Einbahnstrasse

1 second
64 megabytes
Very easy

Einbahnstraße (German for a one-way street) is a street on which vehicles may move only in one direction. One reason for having one-way streets is to facilitate a smoother flow of traffic through crowded areas. This is especially useful in city centers, particularly in old cities such as Cairo and Damascus. Careful planning guarantees that you can get to any location starting from any point. Nevertheless, drivers must carefully plan their route in order to avoid making their trip longer due to one-way streets. Experienced drivers know that there are multiple paths to travel between any two locations. Moreover, there might be multiple roads between the same two locations. Knowing the shortest way between any two locations is essential! This is even more important for vehicles that are hard to maneuver (garbage trucks, towing trucks, etc.).

You have just started a new job at a car towing company. The company owns several towing trucks parked at the company’s garage. A tow truck lifts the front or back wheels of a broken car in order to pull it straight back to the company’s garage. You receive calls from various areas of the city about broken cars that need to be towed. The cars must be towed in the order calls are received. Your job is to advise the tow-truck drivers about the shortest route to collect all broken cars and bring them back to the company’s garage. At the end of the day, you must report to management the total distance traveled by the trucks.

Input

Your program will be tested on one or more test cases. The first line of each test case contains three numbers , , and separated by one or more spaces. The city has locations with unique names, including the company’s garage. is the number of broken cars. is the number of roads in the city. Note that 0 < N < 100, 0 \leq C < 1000, and R < 10000. The second line consists of C + 1 words: the first is the location of the company’s garage, and the remaining are the locations of the broken cars. A location is a word containing at most letters. Letter case is significant.

After the second line, there will be exactly lines, each describing a road. A road is described using one of these three formats:

A -v -> B

A <-v - B

A <-v -> B

and are the names of two different locations, while is a positive integer not exceeding denoting the length of the road. The first format specifies a one-way street from to , the second format specifies a one-way street from to , and the last format specifies a two-way street between them. , the arrow, and are separated by one or more spaces.

The end of the test cases is indicated by a line containing three zeros (for , , and ).

The test case in the example below is the same as the one in the figure.

Output

For each test case, print the total distance traveled in the following format:

.

Where is the test case number (starting at ), followed by a space, and is the result.

Examples

Example 1

Loading
Loading