Under construction

Still being built. Things may change

Palindromic Password

Interview

Time limit3sMemory limit512 MB

Summary
For each six-digit number, print the closest six-digit palindrome, breaking ties by choosing the smaller one.
Level

Easy3 of 10

Topics
Array, Brute force, Math
Solved
No attempts yet

Problem

The IT department at your school changed its password rules. A password is NN six-digit numbers joined by hyphens, and NN depends on the phase of the moon and the next day's weather forecast.

If every one of those six-digit numbers is a palindrome (it reads the same forwards and backwards), you only have to remember NN three-digit numbers. So you take a list of NN randomly generated six-digit numbers and replace each one with the six-digit palindrome closest to it.

Write a program that performs this replacement for you.

Input

The first line contains the count NN of six-digit numbers. (1≤N≤10001 \le N \le 1000)

Each of the next NN lines contains one six-digit number. The number has no leading zero, so it is between 100000100000 and 999999999999.

Output

For each number given in the input, print on its own line the six-digit palindrome closest to it. Closest means the absolute difference with the original number is the smallest. If two palindromes tie for the smallest difference, print the smaller one. The printed number must not have a leading zero either.

Examples7

  1. Example 1

    Input
    2
    123321
    123322
    
    Expected output
    123321
    123321
    
  2. Example 2

    Input
    1
    100000
    
    Expected output
    100001
    
  3. Example 3

    Input
    1
    999999
    
    Expected output
    999999
    
  4. Example 4

    Input
    6
    100551
    100550
    100552
    109956
    109955
    109957
    
    Expected output
    100001
    100001
    101101
    109901
    109901
    110011
    
  5. Example 5

    Input
    6
    199990
    199991
    199996
    199997
    200002
    200003
    
    Expected output
    199991
    199991
    199991
    200002
    200002
    200002
    
  6. Example 6

    Input
    6
    109901
    109902
    109950
    109960
    110010
    110011
    
    Expected output
    109901
    109901
    109901
    110011
    110011
    110011
    
  7. Example 7

    Input
    8
    500000
    549945
    123321
    987789
    100001
    900009
    456654
    111111
    
    Expected output
    500005
    549945
    123321
    987789
    100001
    900009
    456654
    111111