#t130. 进制转换

进制转换

Base Conversion

Problem Description

A base-NN numeral system counts by carrying one whenever NN is reached. For example, people mostly use decimal in daily life, while computers generally use binary at the bottom level. Besides, octal and hexadecimal are also commonly used in some contexts (in hexadecimal, letters A to F usually represent ten to fifteen; similarly in this problem for bases eleven to fifteen).

In this problem we give NN numbers in different bases. You need to convert each of them into a decimal number.

Input Format

The first line of input is an integer NN in decimal. The following NN lines each contain an integer KK, then a space, followed by a base-KK number to be converted. It is guaranteed that all base-KK numbers consist only of digits and uppercase letters, and do not start with 00. The base-KK numbers are guaranteed valid.

Output Format

Output NN lines, each a decimal number: the decimal value of the corresponding base-KK number.

Sample Input 1

2
8 1362
16 3F0

Sample Input 2

2
2 11011
10 123456789

Sample Output 1

754
1008

Sample Output 2

27
123456789

Sample Explanation

In the first sample, there are two lines. The first line's base-88 number 1362 converts to decimal: $1 \times 8^3 + 3 \times 8^2 + 6 \times 8^1 + 2 \times 8^0 = 754$. The second line's base-1616 number 3F0 converts to decimal: $3 \times 16^2 + 15 \times 16^1 + 0 \times 16^0 = 1008$ (where F stands for 1515). So the outputs are 754754 and 10081008 respectively.

Constraints

  • N≤1000N \le 1000;
  • 2≤K≤162 \le K \le 16;
  • all base-KK numbers have at most 99 digits.