MCONVERT - Convert to Decimal Base System


Life can be taught, but sometimes simple problems are just very well hidden among difficult ones. Once identifying those simple problems you are almost on a half way of solving them as well as making one big step towards winning the contest. Just be careful, this is NOT the simplest problem!

Are you ready for that challenge?

Your task is to write a program that transforms numbers from various numeric systems to decimal one (base=10).

Input

Input file consists from multiple data sets separated by one or more empty lines. First line of each set contains definition of digit ordering for some hypothetical numerical system. All ASCII printable characters (codes greater than 0x20 (space)) are allowed to appear as digits, and they are sorted according to increased decimal value (starting from zero).

Each line of the input data set (starting from second one) is one number coded with previously defined digits. Such numbers can have multiple decade interpretations (taking different base for hypothetical system) and your task is to find sum of all possible interpretations.

Explanation: If we define digit ordering as “0123456789” possible bases are 2 .. 10 but number “6201” can be decoded only in systems with base 7 .. 10. Input lines can contain white space characters on both ends which should be ignored.

Output

You are required to output one decimal number per each number from input data sets. That number represents sum of decimal representations for all valid numeric system bases. Output data sets should be separated by one blank line.

Sample

Input:
0123456789
  90763  
1

.1>C
CC.
>.1
1....

Output:
90763
9

60
52
353

Note: big num - answer never has more than 1000 digits.



Added by:psetter
Date:2009-02-27
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ADA95 ASM32 BASH BF C CSHARP CPP C99 CLPS LISP sbcl LISP clisp D FORTRAN HASK ICON ICK JAVA LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYTHON RUBY SCM guile SCM qobi ST WHITESPACE
Resource:Southeastern European 2007