#C5009. Calculate Time Durations in Altrunes's Notation

    ID: 48611 Type: Default 1000ms 256MiB

Calculate Time Durations in Altrunes's Notation

Calculate Time Durations in Altrunes's Notation

You are given a series of test cases. In each test case, a dictionary is provided that maps distinct words to their corresponding time durations. Following the dictionary, an encoded expression is given. The expression is a sequence of words separated by hyphens (-).

Your task is to compute the minimum total time duration for each test case by summing the time durations of the words that appear in the encoded expression.

More formally, if the encoded expression is given by:

\( w_1-w_2-\ldots-w_k \)

and each word \( w_i \) has a duration \( d(w_i) \), then the total time is:

\( \sum_{i=1}^{k} d(w_i) \)

Each test case begins with an integer \( N \) (\( N > 0 \)) which indicates the number of dictionary entries. The next \( N \) lines each contain a word and its duration separated by a space. Then a single line follows with the encoded expression. The input terminates with a test case where \( N = 0 \), which should not be processed.

inputFormat

The input is read from stdin and consists of multiple test cases. Each test case is formatted as follows:

  • An integer \( N \) indicating the number of dictionary entries (if \( N = 0 \), processing stops).
  • \( N \) lines, each containing a word and an integer duration separated by a space.
  • A single line containing an encoded expression of words separated by hyphens (-).

All test cases are concatenated, with the input termination indicated by a line with a single 0.

outputFormat

For each test case, output a line in the format Case i: X where i is the test case number (starting from 1) and X is the sum of the durations of the words found in the encoded expression.

The output should be written to stdout.

## sample
3
blip 5
blink 15
bloop 30
blip-blink
0
Case 1: 20