Under construction

Still being built. Things may change

Roundabout Rounding

Time limit2sMemory limit2048 MB

Summary
Count integers x with 2 <= x <= N where rounding x once to the nearest power of 10 gives a different result than chain rounding through each smaller power first.
Level

Medium6 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

Bessie the cow is back in school. She is doing math homework in which she rounds positive integers to powers of 1010.

To round a positive integer aa to the nearest 10b10^b, where bb is a positive integer, Bessie first finds the bb-th digit from the right. Call this digit xx.

If x≥5x \geq 5, Bessie adds 10b10^b to aa.

Then Bessie sets the bb-th digit from the right and every digit to its right to 00.

For example, to round 456456 to the nearest 10210^2 (hundred), Bessie finds the 2nd digit from the right, which is 55. So x=5x = 5. Since x≥5x \geq 5, Bessie adds 100100 to aa. Then she sets the 2nd digit and every digit to its right to 00, which gives 500500.

Rounding 446446 to the nearest 10210^2 gives 400400.

Elsie thinks she has invented a new type of rounding called chain rounding. To chain round to the nearest 10b10^b, Elsie first rounds to the nearest 10110^1, then to the nearest 10210^2, and so on, until she reaches the nearest 10b10^b.

Bessie thinks Elsie is wrong, but she is too busy with homework to check. You are asked to count the integers xx with 2≤x≤N2 \leq x \leq N for which rounding xx to the nearest 10P10^P gives a different result from chain rounding xx to the nearest 10P10^P, where PP is the smallest integer with 10P≥x10^P \geq x. Here 1≤N≤1091 \leq N \leq 10^{9}.

Input

The first line contains a single integer TT (1≤T≤1051 \leq T \leq 10^5), the number of test cases. Each of the next TT lines contains a single integer NN. All NN values in the same input file are distinct.

Output

Output TT lines. The ii-th line contains the number of integers at least 22 and at most NN for which the two rounding methods give different results, for the ii-th test case.

Hint

Consider the second test case in the sample. 4848 is counted because chain rounding 4848 to the nearest 10210^2 gives 100100 (48→50→10048 \to 50 \to 100), but rounding 4848 to the nearest 10210^2 gives 00.

In the third test case, the two counted integers are 4848 and 480480. 4848 chain rounds to 100100 instead of 00, and 480480 chain rounds to 10001000 instead of 00. However, 6767 is not counted, since it chain rounds to 100100, which is also what rounding 6767 to the nearest 10210^2 gives.

Examples1

  1. Example 1

    Input
    4
    1
    100
    4567
    3366
    
    Expected output
    0
    5
    183
    60