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


hide comments
suchigupta: 2016-08-28 21:22:08

gave correct answer of all test cases(given in comment also) on ideone but on submission SIGSEGV :((

sxdcffg: 2016-08-26 09:59:53

For Python users, if your non-brute force solution is timing out try avoiding converting into lists or an integer. For a 1000000 digit number, these functions become very expensive in terms of time.

imad315: 2016-08-21 20:20:55

what the role of t integer(number of test) ? how can i include it in my code?

leepx: 2016-08-20 05:46:59

You can try this number.
in:39474657
out:39477493

drajingo: 2016-08-19 13:53:03

After 2 SIGSEGVs and 12 WAs, getting that AC was pretty much the best feeling in the world. :D :D
Keep working on it, and test your code with small inputs where the answer can be verified by brute force.

Ashutosh Singh Chandel: 2016-08-05 01:11:51

go to @ vector1996 's comment . helped me..

krishna17292: 2016-08-01 13:55:12

awesome problem , got it finally

heemansh: 2016-07-28 10:42:36

getting TLE ..here but on Dev C it is working properly
help

jigarjani: 2016-07-22 07:41:55

All test cases running on eclipse and ideone (java).. In spoj, NZEC runtime error. HELP PLSSS !!!! Its super frustrating..

mxfce: 2016-07-19 12:24:11

Don't use Big Integer its too slow , I suggest you use int array to store digits. when K =191 makes sure its output is 202 not 101. GOOD LUCK!


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

Problem's scores 1 vote

Concept difficulty
Concept difficulty 37%
Implementation difficulty
Implementation difficulty 50%
467 16