Sum Squared Digits Function

For each of P datasets, convert the given integer n to base b, square each digit, and report the total with the dataset number.

Easy2MathImplementationNumber theoryInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Write a positive integer nn in base bb:

n=a0+a1b+a2b2+n = a_0 + a_1 b + a_2 b^2 + \cdots

The sum squared digits function SSD(b,n)\mathrm{SSD}(b, n) is the sum of the squares of the digits of that representation.

SSD(b,n)=a02+a12+a22+\mathrm{SSD}(b, n) = a_0^2 + a_1^2 + a_2^2 + \cdots

Write a program that computes the sum squared digits function of a given positive integer.

Input

The first line contains one decimal integer PP, the number of data sets that follow. (1P100001 \le P \le 10000)

Each of the next PP lines holds one data set. The data sets are independent and are all processed the same way.

A line contains the data set number KK, the base bb (3b163 \le b \le 16), and the positive integer nn whose sum squared digits value in base bb is to be computed, separated by spaces. All three values are decimal integers, and nn fits in a 32 bit unsigned integer.

Output

Print one line for each data set.

The line contains the data set number KK, a single space, and the value of SSD(b,n)\mathrm{SSD}(b, n) as a decimal integer.