PALIN - The Next Palindrome

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

Input:
2
808
2133

Output:
818
2222

Warning: large Input/Output data, be careful with certain languages


Added by:adrian
Date:2004-05-01
Time limit:2s-9s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6

hide comments
2015-11-15 16:25:27
Confirmed that there are nothing wrong with the test cases. If u get a WA then there's some problem with ur code. I literally used an hour to debug just to reach an Accepted. Try 999, 8997, 3, 9, 100, 1000, 9999 beforehand.
2015-11-12 11:58:38 Priyank
First make a simple ago. (just for handling int) then make it more sophisticated for handling large numbers.

Last edit: 2015-11-12 11:59:10
2015-11-09 08:01:11
@abhi_269, go try "999999999999".
2015-11-04 05:58:28
"digits"
2015-11-03 15:15:10 LeppyR64
FYI: Almost everyone that posts on the forum for this problem skips exactly one word in this problem statement.

Last edit: 2015-11-03 15:15:31
2015-10-27 14:02:50
Very nice adhoc problem :) Got AC in first submission here but after many debugging. Had to generate a bruteforce solution of 1 to 10000 numbers and then match with my approach to find corner cases.
2015-10-25 20:59:17
Hahaha, accepted on the first go!! Love the problem!
2015-09-07 20:13:18
good for beginner ..!!!
2015-09-03 21:57:01 MSK
Very small time constraint for Java :(
2015-08-16 22:34:14
Hi, for some unknown reason, exactly the same code produces an AC when using "Python 3 (python 3.4)" and a WA when using "Python3.4 (Python 3.4)".
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.