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
ashrockzy: 2018-10-19 07:19:21

any alternatives to cin and cout in c++ ?

dragost_305: 2018-10-11 19:34:18

getting non zero exit error in java though all cases are working properly in netbeans

ben_d: 2018-10-09 16:56:26

Getting time exceeded even with fail-fast implementation in java...

mr_volatile: 2018-10-09 16:00:12

In my problem, default test case and test case '9' is solved upto 14 times '9', But still getting wrong answer.

xoker: 2018-10-08 14:53:50

Very nice problem spent lot of time to solve. one thing is k=1000000 digits.. so you have to solve it using string

masterchef2209: 2018-10-07 17:53:12

good question, simple using string manipulation :D




[SPOILER BELOW]
corner case:
when all digits are 9

Last edit: 2018-10-07 17:54:15
n1trob4ct3r: 2018-09-28 19:33:10

rogu_auto you can use int to store K as it is only 10^6 wheras integer limit in c++ is ~10^9.

eagleshadow: 2018-09-28 19:18:27

AC in one go :)

dworack: 2018-09-19 07:48:53

Wrong answers a few times. After changing fgets to scanf, the same code works well! (language C).

dadhich_01: 2018-09-02 17:55:14

naruto17 read question clearly


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