Subway Fares

Interview

Time limit1sMemory limit128 MB

Summary
Given a per-stop fare table and an ordered list of station names, find the number of stops between two stations and print the corresponding fare.
Level

Easy3 of 10

Topics
Array, String, Implementation, Hash map
Solved
No attempts yet

Problem

One of the first questions a subway traveler asks is: how much will the trip cost? Presenting this clearly should be simple, yet in practice it can mean looking up several tables posted around the station and then doing awkward arithmetic. Let's write a program that computes it instead.

We focus on a single subway line. You are given a price table stating how much a trip of ss stops costs, for each s≥1s \ge 1. You are also given the names of the stations in order, together with your starting and ending station. Compute the fare. Note that the line can be traveled in either direction, so only the number of stops between the two stations matters.

Input

The first line contains the number KK of data sets. Each of the KK data sets has the following form.

The first line of a data set contains an integer nn (2≤n≤502 \le n \le 50), the number of stops on this line. The next n−1n - 1 lines describe the price table: the ss-th of these lines contains an integer, the price of traveling ss stops (s=1s = 1 means getting off at the stop immediately after boarding). The next nn lines each contain the name of a station, listed in order along the line; every name consists of lowercase letters only and all names are distinct.

The last two lines contain the name of your starting station and the name of your ending station. Both appear in the station list and differ from each other.

Output

For each data set, print the line Data Set x:, where x is the number of the data set (starting from 1), followed by a line containing the fare for the trip. Separate consecutive data sets with a single blank line.

Examples1

  1. Example 1

    Input
    2
    3
    1
    13
    unionstation
    usc
    culvercity
    unionstation
    usc
    4
    2
    3
    4
    citycenter
    hollywood
    ucla
    beach
    beach
    hollywood
    
    Expected output
    Data Set 1:
    1
    
    Data Set 2:
    3