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
2020-05-24 00:26:44
0 -> 1
1 -> 2
2 -> 3
...
9 -> 11
99 -> 101
999 -> 1001
9898 -> 9999
2020-05-17 16:48:29
There are no leading zeros.
2020-05-16 21:00:15
Try for these test cases
0012100-->12121
0003-->4

You need to get rid of the leading zeros
2020-05-10 18:28:28
I've got AC and I don't think that the implementation is difficult. Nice C++ code needs less than 80 lines of code.

Last edit: 2020-05-10 18:29:45
2020-05-10 03:34:49
Thanks @trulyness for the extra test cases
Got some wa because of not having awared of them.

Last edit: 2020-05-10 03:36:16
2020-05-09 05:30:08
can anyone explain what problem says?
2020-05-07 16:17:01
do i just increment the input by one and check if its a palindrome or is there a better and a faster method?
2020-05-07 06:23:05
Its logic is easy but implementation is hard.
It took me one complete day to make logic that covers all cases and then AC in 1 go.
2020-05-04 01:09:54
When they say, that elements are not more than 1000000 digits, they are not joking, my char array in C was one element to short

Last edit: 2020-05-04 01:17:10
2020-04-25 09:54:46
guys, there is space character after input in every line, just take care of it
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.