Under construction

Still being built. Things may change

Kakezan

Time limit3sMemory limit512 MB

Summary
For each nonnegative integer, repeatedly cut its decimal representation at some point, replace it with the largest product of the two parts, and count steps until it becomes one digit, or report -1 if it never does.
Level

Medium7 of 10

Topics
Dynamic programming, Math, Implementation, Brute force
Solved
No attempts yet

Problem

Taro is an elementary school student who has just learned multiplication. Somehow he has taken a liking to it, and whenever he sees a number he wants to multiply. Lately he seems to enjoy applying the following procedure to nonnegative integers.

  • Step 1. If a nonnegative integer nn has one digit in decimal, the procedure ends. Otherwise, go to step 2.
  • Step 2. When a integer n≥10n \ge 10 is written in decimal, it can be split into two numbers by cutting between any two digits (for example, 2012→20,122012 \to 20, 12). Among all such cuts, take the two resulting numbers, multiply them, and use the largest product as the next nn, then return to step 1. See "Notes on step 2" below for details.

Taro likes this procedure, but for large numbers he cannot predict how many times step 2 must be repeated, and he thinks it might even have to be repeated infinitely many times. So he asked you, his older brother in university, how many times step 2 must be performed for a nonnegative integer nn.

Your task is, given QQ nonnegative integers N1..NQ, to determine for each integer how many times step 2 is performed before the procedure ends. If infinitely many repetitions are needed, output -1.

When splitting, cases where the result has a leading zero must also be considered. For example, when n=1024n=1024, computing 1∗0241*024, 10∗2410*24, and 102∗4102*4 gives 24, 240, and 408 respectively, so 408 is chosen and becomes the next nn.

Input

Q
N1
N2
...
NQ
  • Q is the number of nonnegative integers given.
  • Ni is the i-th nonnegative integer Taro is curious about.

Output

Output Q integers separated by newlines.

R1
R2
..
RQ
  • Ri is the number of times step 2 is executed for Ni before the procedure ends.
  • If step 2 must be executed infinitely many times for Ni, Ri is -1.

Constraints

  • 1≤Q≤100
  • 0≤Ni≤106

Examples2

  1. Example 1

    Input
    3
    9
    99
    123
    
    Expected output
    0
    2
    3
    
  2. Example 2

    Input
    2
    999999
    1000000
    
    Expected output
    12 
    1