This page is still under construction.

Parts of this page are still being built. What you see may change.

Hill Numbers

Time limit5sMemory limit256 MB

Summary
Given N with up to 70 digits, count hill numbers smaller than N, or print -1 when N is not one.
Level

Medium6 of 10

Topics
Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

A hill number is an integer whose digits may rise and then fall, but never fall and then rise again.

  • 12321 is a hill number.
  • 101 is not a hill number.
  • 1111000001111 is not a hill number.

Two neighboring digits may be equal, before the top or after it. Only integers of 0 or more are counted, and 0 is a hill number.

An integer NN is given. If NN is a hill number, print how many hill numbers are smaller than NN. If it is not, print −1-1.

Input

The first line has the number of test cases TT (1≤T≤1001 \le T \le 100).

Each of the next TT lines has one integer NN. NN is at least 1 and has at most 70 digits. The answer always fits in a signed 64-bit integer.

Output

Print the answer for each test case on its own line.

Examples2

  1. Example 1

    Input
    5
    10
    55
    101
    1000
    1234321
    
    Expected output
    10
    55
    -1
    715
    94708
    
  2. Example 2

    Input
    9
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Expected output
    1
    2
    3
    4
    5
    6
    7
    8
    9