Thai Lottery Prize Check

No attempts yetTime limit2sMemory limit256 MB

Problem

Write a program that computes the prize money of a Thai government lottery ticket. A ticket number has six digits. Six prizes are checked: one first prize, two front three digit prizes, two last three digit prizes, and one last two digit prize.

A ticket wins each prize under these conditions.

  • First prize: all six digits of the ticket equal the winning number.
  • Front three digits: the first three digits of the ticket equal the winning number.
  • Last three digits: the last three digits of the ticket equal the winning number.
  • Last two digits: the last two digits of the ticket equal the winning number.

When one ticket wins several prizes, add every prize it wins. If two prizes have the same winning number, the ticket receives both.

Input

The first six lines each hold a winning number and a prize amount separated by a space. The order is the first prize, the two front three digit prizes, the two last three digit prizes, and the last two digit prize. The first prize number has six digits, the front three digit and last three digit numbers have three digits, and the last two digit number has two digits. Leading zeros are written out. Each prize amount is an integer between 1 and 10910^9.

From the seventh line on, each line holds one six digit ticket number to check. A line containing only -1 ends the input. There are at most 100,000 ticket numbers.

Output

For each ticket, in input order, print on its own line the total prize money that ticket receives.