License plates#4059

License plates

0.4 seconds
16 megabytes
Very easy

The rapid increase in the number of cars in the country has resulted in a shortage of license plates.

A committee was established to design a new plate format, and they decided on the following guidelines:

Your task is to determine the maximum number of unique plates that can be generated based on the character groups and their designated mask.

Input

The first line contains a single integer – the number of character groups (1 \leq n \leq 9).

The following lines describe the character groups. Each line includes two integers and , separated by a space, where is the group number (1 \leq k \leq n), and is the number of characters in that group (1 \leq c \leq 15).

The last line contains the plate mask, where each digit indicates the group number from which a character must be placed in the corresponding position. The mask has at least one character and does not exceed characters in length. It is guaranteed that each digit in the mask is between and .

Output

The output should be a single integer representing the maximum number of unique plates that can be created under these conditions. If no plate can be created, output .

Examples

Example 1

Loading
Loading