Under construction

Still being built. Things may change

What a Twist

Time limit1sMemory limit128 MB

Summary
Find the maximum of n times its digitwise nine-complement over all integers from 1 to N for each test case.
Level

Medium5 of 10

Topics
Math, Greedy
Solved
No attempts yet

Problem

The reversal F(n)F(n) of a positive integer nn is the number you get by replacing every digit aa of nn with 9−a9 - a.

Zeros that end up in front of the leading significant digit are ignored. So the reversal of 9 is 0, the reversal of 91 is 8, the reversal of 124 is 875, and the reversal of 990 is 9.

The loveliness of nn is nn multiplied by F(n)F(n).

The loveliness of 124 is 124 times 875, which is 108500

Given a positive integer NN, find the largest loveliness among the numbers from 1 to NN.

Input

The first line contains the number of test cases TT. (1≤T≤200001 \le T \le 20000)

Each of the next TT lines contains one positive integer NN. (1≤N≤10000000001 \le N \le 1000000000)

Output

For each test case, print on its own line the largest loveliness among the numbers from 1 to NN. The answer for the kk-th test case goes on the kk-th line.

Hint

Both 4 and 5 have loveliness 20, and 100 has loveliness 89900.

Examples6

  1. Example 1

    Input
    2
    7
    100
    
    Expected output
    20
    89900
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    8
    
  3. Example 3

    Input
    10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    Expected output
    8
    14
    18
    20
    20
    20
    20
    20
    20
    890
    
  4. Example 4

    Input
    10
    9
    10
    49
    50
    99
    100
    499
    500
    999
    1000
    
    Expected output
    20
    890
    2450
    2450
    2450
    89900
    249500
    249500
    249500
    8999000
    
  5. Example 5

    Input
    10
    4
    5
    44
    45
    449
    450
    4499
    4500
    44999
    45000
    
    Expected output
    20
    20
    2420
    2430
    246950
    247050
    24744500
    24745500
    2474945000
    2474955000
    
  6. Example 6

    Input
    1
    1000000000
    
    Expected output
    8999999999000000000