This page is still under construction.

Parts of this page are still being built. What you see may change.

Thai Lottery Prize Check

Time limit2sMemory limit256 MB

Summary
Given the winning numbers and prize amounts for six Thai lottery categories, compute the total prize for each ticket by adding every matched category.
Level

Easy1 of 10

Topics
String, Implementation
Solved
No attempts yet

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.

Examples5

  1. Example 1

    Input
    021094 3000000
    260 2000
    403 2000
    068 2000
    819 2000
    89 1000
    100967
    403094
    260809
    201216
    198025
    260989
    123819
    195800
    021094
    008589
    -1
    
    Expected output
    0
    2000
    2000
    0
    0
    3000
    2000
    0
    3000000
    1000
    
  2. Example 2

    Input
    123456 6000000
    123 500000
    123 400000
    456 300000
    456 200000
    56 100000
    123456
    123999
    999456
    000056
    -1
    
    Expected output
    7500000
    900000
    600000
    100000
    
  3. Example 3

    Input
    000000 1000000
    000 4000
    001 5000
    000 6000
    002 7000
    00 8000
    000000
    000123
    100000
    123400
    -1
    
    Expected output
    1018000
    4000
    14000
    8000
    
  4. Example 4

    Input
    999999 1
    000 1
    111 1
    222 1
    333 1
    44 1
    123456
    -1
    
    Expected output
    0
    
  5. Example 5

    Input
    111111 1000000000
    111 1000000000
    222 1000000000
    111 1000000000
    333 1000000000
    11 1000000000
    111111
    222111
    111333
    101011
    -1
    
    Expected output
    4000000000
    3000000000
    2000000000
    1000000000