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
omp720100: 2016-06-10 16:31:10

what is the answer to 0000000 is it 1 or 0001000? plzzz someone explain it to me!!!!!!!!!!!!

blueranger: 2016-06-10 08:04:39

Very good prblm!!
Hint: jst make a middle palindrome substring greater palindrome and copy remaining letters from left to right side!!

mkfeuhrer: 2016-06-03 11:14:56

nice one....break from mid and continue end ways :-) A.C. :-)!!

dibid: 2016-05-29 09:37:53

Do we have to check the size of the input? in case it's more than 100000 digits?

dibid: 2016-05-29 08:24:18

what is the answer to 0000000 is it 1 or 0001000?

ragwave: 2016-05-24 16:18:50

Finally accepted after 2 WA! check for tricky cases:
input:
7
24
9
0000000
999999
2456
2998
29999
output:
33
11
0001000
1000001
2552
3003
30003

PS:You won't get any points for solving this :P

Last edit: 2016-05-24 16:19:10
Kirill Diduk: 2016-05-24 14:57:36

In this problem, it is expected that a single-digit number is NOT a palindrome. So, when you get a single-digit number as an input, the output should be 11.

xinnix: 2016-05-21 14:47:44

Wth! Working everytime in my compiler but says wrong output here.... What's going on?
Edit: Ok.... I read 1000000 not 1000000 'digits'.... Oopsss

Last edit: 2016-05-21 14:50:04
avisheksanvas: 2016-05-18 08:51:54

Be careful while comparing.
Put ' ' between numbers. Cost me 7 WA's :p

Gaurav Kumar: 2016-05-12 14:37:59

hey , Is 1000 a palindrome ?
0001000 maybe


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