The kingdom of Numeria is very proud of the quality of its numbers, so it charges its citizens a tax for every change made to a number. Even so, the people of Numeria love transforming numbers.
A group of friends, the Units, use the cheapest possible transformations. A number is written in decimal without leading zeros, and only its first (most significant) or last (least significant) digit may be changed:
The Units always start from the number $1$.
For example, $2021$ can be obtained from $1$ with the following sequence, costing $14$ gold coins:
In the diagram below, the number above each arrow is the cost of that step and the expression below it is the operation applied.
$$ 1 \underset{1 +1}{\overset{1}{\Longrightarrow}} 2 \underset{2 \times 5}{\overset{2}{\Longrightarrow}} 10 \underset{1 +1}{\overset{1}{\Longrightarrow}} 20 \underset{2 \times 5}{\overset{2}{\Longrightarrow}} 100 \underset{1 \times 2}{\overset{2}{\Longrightarrow}} 200 \underset{0 +1}{\overset{1}{\Longrightarrow}} 201 \underset{1 \times 5}{\overset{2}{\Longrightarrow}} 205 \underset{5 \times 4}{\overset{2}{\Longrightarrow}} 2020 \underset{0+1}{\overset{1}{\Longrightarrow}} 2021 $$
But $2021$ can also be reached more cheaply, for only $9$ gold coins:
$$ 1 \underset{1 \times 9}{\overset{2}{\Longrightarrow}} 9 \underset{9 \times 5}{\overset{2}{\Longrightarrow}} 45 \underset{4 \times 5}{\overset{2}{\Longrightarrow}} 205 \underset{5 \times 4}{\overset{2}{\Longrightarrow}} 2020 \underset{0+1}{\overset{1}{\Longrightarrow}} 2021 $$
Help the Units obtain $M$ given numbers using these transformations.
For each of the $M$ numbers $A_i$, find the least cost for which the Units can obtain $A_i$ starting from $1$. If a number cannot be obtained by any sequence of these transformations, its answer is $-1$.
The first line contains an integer $M$ — the count of numbers in the set. Each of the next $M$ lines contains one natural number $A_i$ ($1 \le i \le M$).
Print $M$ lines. On the $i$-th line print the least cost of the unit transformations that produce $A_i$ from $1$. If no such transformations exist for a given number, print $-1$ on its line.