spoj#SUFFIX. Suffixes

Suffixes

Find the smallest natural number X such that, if we write X in bases B1, B2, ..., BN, we get strings with suffixes S1, S2, ..., SN, respectively.

The possible digits are 0123456789ABCDEFG...XYZ, with values 0..35. Of course, the number written in base B consists only of the digits with values between 0 and B - 1.

Input

In the first line of input there is an integer N (1 ≤ N ≤ 10) from the task description.

In kth of the next N lines there is an integer Bk (2 ≤ Bk ≤ 36) and a suffix Sk from the task description.

The given bases will be pairwise distinct. Also, the product of the powers Bk ^ lenght(Sk) will be less than 1018.

Output

Print the required X, written in base 10.

Example

input
3
5 22
11 A2
18 4

output 112

</p>
input
5
2 110
32 E
25 M3
28 2
7 2

output
53678