Setting

The “reverse and add” method is simple: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure. For example:

195 Initial number
591
—–
786
687
—–
1473
3741
—–
5214
4125
—–
9339 Resulting palindrome

In this particular case the palindrome ‘9339’ appeared after the 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome. You must write a program that give the resulting palindrome and the number of iterations (additions) to compute the palindrome.

You might assume that all tests data on this problem:

  • will have an answer,
  • will be computable with less than 1000 iterations (additions),
  • will yield a palindrome that is not greater than 4,294,967,295.

Input

The first line will have a number N (0 < N <= 100) with the number of test cases, the next N lines will have a number P to compute its palindrome.

Output

For each of the N tests you will have to write a line with the following data: minimumNumberOfIterationsToGetToThePalindrome and TheResultingPalindromeItself separated by one space.

Sample Input

3
195
265
750

Sample Output

4 9339
5 45254
3 6666

This challenge is provided by the ACM International Collegiate Programming Contest.

Upload Solution

Please log in to submit your solution.

Statistics

Difficulty (3 votes)
Average test runtime 0.25
Points (changes over time) 10
Tried by 21 users
Solved by 20 users

Global ranking

# Name Runtime Points worth
1 ,s/java/NaN/gi 0.13 17
2 Justin 0.14 16
3 mascent 0.15 15
4 Pascal 0.16 14
5 Irfan 0.16 14
6 Jan 0.20 11
7 Melf 0.20 11
8 maltesar 0.23 10
9 Phatti McButterpants 0.24 9
10 JB 0.24 9
11 Finn-Lasse 0.24 9
12 Brian 0.24 9
13 Dark 0.26 9
14 Mac 0.27 8
15 Nis 0.32 7
16 Soeren 0.33 7
17 Birger 0.37 6
18 IeM 0.38 6
19 Eren B. Yilmaz 0.39 6
20 Skøgland 0.39 6