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
2019-06-09 07:32:18
i'm getting runtime error in java still i'm using string to accept number and long for increment so i'm not getting why it is showing runtime error any body has any idea
2019-05-23 14:06:41
999->1001
2019-05-13 06:40:00
Help? Getting SIGABRT.<snip> It seems like its passing all the test cases mentioned here in the comments though.

Last edit: 2022-07-26 22:15:02
2019-04-28 15:22:12
AC - 0.04s
1.Don't try generating palindromes.
2.Take input as string and edit it to form a palindrome.
3.Replicate right part same as left part. compare with input if still it isn't greater then add 1 to right most number in you left part and replicate the same on right part. (Don't forget to handle odd length case)
Test cases :
123456 -> 124421
1234567 -> 1235321
9999 -> 10001
99999 -> 100001
1 -> 2
9 -> 11
2019-04-18 18:49:26
AC in one go :) but 2.76 time in JAVA, can it be faster in java?
2019-04-18 18:46:40
few interesting test case:
2991
3994
9999
5448
65343454
etc
2019-04-09 12:54:09
I tested 4 scenarios: all 9s, the number itself is a palindrome, numbers like 54321 that just need to mirror the left side to right side and numbers like 12345 that need to mirror the left side to right side and deal with carries, my code passed all 4 cases and work with 1 digit numbers and numbers with leading zeros, but I still failed, any help?

Last edit: 2019-04-10 04:46:08
2019-04-03 11:10:18
I need your help. I don't known where i wrong
2019-04-02 11:25:50
I guess all my test cases are correct..but it shows time limit exceeded..what should I try next??

Last edit: 2019-04-02 11:27:16
2019-03-28 18:19:31
0.06s
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.