`JBC`#791

`JBC`

1 second
64 megabytes
Hard

Life can be learned, but sometimes simple problems are well hidden among difficult ones. Once you identify those simple problems, you are halfway to solving them and making one big step toward winning the contest. Be careful: this is NOT the simplest problem!

Are you ready for this challenge?

Your task is to write a program that converts numbers from various numeral systems to decimal (base ).

Input

The input consists of multiple data sets separated by one or more blank lines.

The first line of each set defines the digit order for a certain hypothetical numeral system. All printable ASCII characters (codes greater than x (space)) can be used as digits and are sorted in order of increasing decimal value (starting from zero).

Each line of the data set (starting from the second) is a number encoded using the previously defined digits. Such numbers may have multiple decimal interpretations (by considering different bases for the hypothetical system). Your task is to find the sum of all possible decimal interpretations.

Explanation: If we define the digit order as "", possible bases are , but the number "" can be decoded only in systems with bases .

Input lines may contain whitespace at both ends, which must be ignored.

Output

For each number in the input data sets, output one decimal number: the sum of its decimal values for all valid bases of the numeral system.

The output data sets must be separated by a single blank line.

Examples

Example 1

Loading
Loading