We are given a process: start with integer n (non-negative). If n is a single digit (0 <= n <= 9), stop. Otherwise, we consider all ways to split the decimal representation of n into two non-empty parts (i.e., break between some digits, resulting in two strings of digits, possibly with leading zeros). For each split, we compute the product of the two numbers (the numbers as integers, with leading zeros allowed, so e.g., "024" is 24). Then we take the maximum product among all splits, and set n to that product. Then repeat from step 1.
We need to compute, for each starting n, the number of times step 2 is performed until termination (i.e., until we get a single-digit number). If the process never terminates (i.e., goes into an infinite loop or infinite steps), output -1.
Constraints: Q up to 100, Ni up to 10^6 (i.e., max 7 digits). However, after step 2, n might become larger? Let's think: splitting a number into two parts and multiplying them. For example, starting with 999999, output says 12 steps. Could product become larger than original? Possibly, but since we split and multiply, product could be larger or smaller. For example, 99 splits: 99=81, max product is 81, which is less than 99. For 123, splits: 123=23, 123=36, max=36; then 36 splits: 36=18, 36? Actually 36 splits: 36=18, and 36? Wait splits: break after first digit: 3 and 6 -> product 18; break after second: 36 and? Actually 2-digit number can only split once: between first and second digit, so only product 36=18. So 36 -> 18. 18 splits: 18=8, 18? Actually 18=8 only, so ->8. So steps: 123->36 (1), 36->18 (2), 18->8 (3). So total 3 steps. Good.
Now, we need to see if infinite loops can occur. The process is deterministic: given n, we compute a unique next n = f(n) = max product over all splits. So it's a functional iteration. For n >= 10, f(n) is some integer. Could it be that the iteration never reaches a single digit? That would require that f(n) >= 10 forever, and that there is no cycle that avoids single-digit numbers? Actually, if it enters a cycle (all numbers >=10), then it never reaches a single digit, so infinite steps. Also if it diverges to infinity? But numbers might become larger? Possibly product can be larger than original? For example, n=101: splits: 101=1, 101=10, max=10. So 101->10. 10->? splits: 10=0, 10? Actually for 10, break after first digit: 1 and 0 -> product 0; break after second? Not allowed because second part empty? Actually "break between some digits": we need two non-empty parts. So for 2-digit number only one split: between digits. So 10 -> 10=0. So 0 is single digit? Yes, 0 is a digit (single digit). So 101 leads to 0 after 2 steps. So not infinite.
Consider n=1000: splits: 1000=0, 1000=0, 1000=0, 1000? Actually 1000 splitting into two parts: possible break positions: after first digit: "1" and "000" -> product 10=0; after second: "10" and "00" -> product 100=0; after third: "100" and "0" -> product 1000=0. So max product = 0. So goes to 0. So many zeros.
But can we get product that is large? For example, n=999 -> 999=891, 999=891? Actually splits: 999=891, 999=891 same. So 891. Then 891: splits: 891=728, 891=89, max 728? Actually 891=728, 891=89 -> max 728. Then 728: 728=196, 728=576 -> max 576; 576: 576=380, 576=342 -> max 380? Actually 576=342, 576=380, max 380; 380: 380=240, 380=0 -> max 240; 240: 240=80, 240=0 -> max 80; 80: 8*0=0 -> 0; etc. So eventually goes to 0.
Potentially some numbers might cause infinite loop? For example, if we ever get a number like 10, we go to 0; 0 stops. So maybe all numbers eventually terminate? But we must check possibility of cycle: for n>=10, f(n) could be again n? That would be a fixed point. But if n is 2-digit, f(n) = product of its digits. Could that equal n? For 2-digit n = a10+b, product = ab. Solve ab = 10a+b => b = (10a)/(a-1)? Not integer? Check: 10a+b = ab => 10a = a*b - b = b(a-1) => b = 10a/(a-1). For a from 1 to 9, a-1 divides 10a? Let's test: a=1: b=10/0 undefined; a=2: b=20/1=20 not digit; a=3: b=30/2=15; a=4: b=40/3 13.33; a=5: b=50/4=12.5; a=6: b=60/5=12; a=7: b=70/611.67; a=8: b=80/7~11.43; a=9: b=90/8=11.25. So no. So no 2-digit fixed points.
For numbers with more digits: Could f(n) = n? That would mean the maximum product over splits equals n itself. For that to happen, there must be a split such that product = n, and that product is max, and we set n to that product, so it stays same. For example, if we have a number like 00? Not possible. But maybe some numbers like 1? Already single digit. Could there be a cycle of length >1? For example, n1 -> n2 -> n1? Let's test small numbers manually. But we should reason: Since we multiply two positive integers (except when leading zeros produce zeros), product tends to be less than the original number? Not always: Example: 1234: splits: 1234=234, 1234=408, 1234=492. Max=492 which is less than 1234. 2222: 2222=444, 2222=484, 2222=444, max 484 < 2222. 1009: 1009=9, 1009=90, 1009=900, max 900 < 1009. 1010: 1010=10, 1010=100, 1010=0, max 100 < 1010. 1099: 1099=99, 1099=990, 1099=981, max 990 < 1099. 1900: 1900=900, 1900=0, 1900=0, max 900 < 1900. 1999: 1999=999, 1999=1881, 1999=1791, max 1881 < 1999. 9999: 9999=8991, 9999=9801, 9999=8991, max 9801 < 9999. 10000: 10000=0, 10000=0, 10000=0, 10000=0, max 0 < 10000. So seems f(n) < n for all n >= 10? But is it always? Let's consider numbers with leading zeros after split? The split parts can have leading zeros; e.g., n=101, we had 101=10, which is less than 101; 101=1. So yes. But what about numbers like 99? 99=81 <99. So f(n) < n for n>=10? Check n=10: f=0 <10; n=11: 11=1<11; n=12: 12=2<12; n=20:20=0<20; n=90:90=0<90. n=100: max split: 100=0? Actually 100=0, 100=0, 100? cannot split into 100 and ''; so max=0<100. n=101: max=10<101. n=102: 102=20, 102=2, 102? can't; max=20<102. n=110: 110=10, 110=0, 110?; max=10<110. n=111: 111=11, 111=11, max=11<111. n=125: 125=60, 125=25, 125?; max=60<125. n=250: 250=0, 250=100, 250?; max=100<250. n=256: 256=150, 256=112, 256?; max=150<256. n=999: 999=891, 999=891, max=891<999. So seems f(n) < n for all n>=10. Is there any n where product could be equal or greater? Suppose we split n into a and b, where a is the left part and b the right part. If n has d digits, then n = a * 10^{len(b)} + b. The product ab. Compare ab to n. Since a10^{len(b)} is huge, ab would be less than a10^{len(b)} unless b is zero? Actually n = a10^{k} + b, where k = len(b) >=1. Then ab <=? Compare ab to n: n - ab = a10^{k} + b - ab = a(10^{k} - b) + b. Since b < 10^{k} (as it has k digits, but with possible leading zeros, b can be as small as 0 up to 10^{k}-1). So 10^{k} - b >=1, thus n - a1 + b > 0 (unless a=0 and b=0? But a cannot be zero because it's a non-empty string with possibly leading zeros? Actually a is the left part, it can have leading zeros only if the original number had leading zeros? But original number is given without leading zeros (standard decimal representation). However, when we split, we consider the original string of digits; the left part is the prefix of the original string. Since the original string has no leading zeros (except for the number 0 itself which is single digit), the left part's first digit is non-zero. So a >= 10^{len(a)-1} > 0 for len(a)>=1; if len(a)=1, a is digit 1-9. So a>0 always. And b can be zero. Then n - a10^{k} + b - a(10^{k} - b) + b > 0 because a>0 and (10^{k} - b) >=1 unless b = 10^{k}? But b < 10^{k} because b has at most k digits; it could be exactly 10^{k} if it had k+1 digits, but it doesn't. So (10^{k} - b) >=1. So indeed n > a*b always. Therefore, for any split, the product is strictly less than n. Since f(n) is the max over splits, f(n) < n for all n >= 10.
Thus the sequence n, f(n), f(f(n)), ... is strictly decreasing (in terms of integer values) as long as it stays >=10. Since it's a strictly decreasing sequence of non-negative integers, it must eventually become less than 10 (i.e., single digit) after a finite number of steps. Therefore, the process always terminates for all starting n. So there is no infinite loop or infinite steps. But wait, the problem statement says: "If you need an infinite number of steps, output -1." That implies that under some numbers it might not terminate. However, from our reasoning, it seems always terminates because f(n) < n. But could there be a case where f(n) = n? We proved that for any split, product < n, so f(n) < n. So strictly decreasing, so it must eventually hit a number less than 10. Since it's decreasing, it cannot cycle. So termination is guaranteed. But wait: check n=0? 0 is single digit, stop. So indeed always terminates.
But perhaps we need to consider the case where n is negative? No, n >= 0. So why does the problem mention possibility of infinite steps? Could there be an edge case: If n is a single digit, step 2 not executed. But if n >= 10, we go to step 2. For some numbers, could we get f(n) = n? Not from product, but could there be a scenario where the maximum product is not defined? No, there is always at least one split (since at least two digits). So f(n) is defined and less than n.
Maybe the problem includes the possibility of numbers with leading zeros after split? But we accounted for that. Our inequality n > ab holds even if b has leading zeros? Let's be careful: The original number n is represented as a string of digits without leading zeros (except for 0). When we split at some position, we get two strings: left string s, right string t. The integer value of left part is a = int(s). Since s is a prefix of the original number, and the original number has no leading zeros, the first digit of s is the first digit of the original number, so it's non-zero. So a >= 10^{len(s)-1}. Good. The right part t may have leading zeros; its integer value b = int(t) satisfies 0 <= b <= 10^{len(t)}-1. But note: t could be all zeros, then b=0. And importantly, the numeric value of n = a * 10^{len(t)} + b. Since b <= 10^{len(t)}-1, we have 10^{len(t)} - b >= 1, so n - ab = a*(10^{len(t)} - b) + b >= a1 + b > 0, because a>0. So indeed n > ab.
Thus f(n) < n for all n >= 10.
Therefore, the process always terminates. So for all Ni, we can compute finite steps; never need to output -1. But the problem statement includes -1 for infinite steps, maybe because of some nuance: maybe for n=0? No, single digit stops. Maybe for numbers like 10^6? Not infinite. So maybe there is a catch: In step 2, "it is possible to break it into two numbers by inserting a break between some digits (for example, 2012-> 20, 12). For possible cutting methods like this, multiply the two obtained numbers and set the largest one as the next n". But maybe they consider that when splitting, we treat the resulting numbers as strings without removing leading zeros, and then we interpret them as numbers. That's what we did. So still product < n.
Wait, but consider the scenario where n = 0? Already single digit. n = 1-digit? Done.
Thus, the only reason to output -1 would be if some number leads to an infinite loop where step 2 repeats forever without reaching a single digit. But given f(n) < n, that cannot happen. However, maybe they consider that if n becomes a number with more than one digit and we repeat, the process could become infinite if the product ever becomes equal to n? But that's impossible as argued. But could there be a situation where n becomes a number with leading zeros after product? For example, product = 024 is 24, but we treat as integer 24, not as string with leading zeros. So representation does not have leading zeros. So fine.
Maybe the -1 is for numbers like 10? But 10 -> 0, stop. So not.
Thus, I'm confident that the process always terminates. But the problem statement says: "If you need an infinite number of steps, output -1." Possibly they had some tricky case like 0? Or maybe they considered that for n=0, it's single digit, so steps 0. So no. So I think we can ignore -1; but to be safe, maybe there is an edge case where the product is again a multi-digit number and we get a cycle? But since strictly decreasing, cannot cycle.
Double-check with some random numbers to see if product could be bigger: For n=20, split: 20=0, product=0 <20. For n=101, product max=10 <101. For n=210, splits: 210=20, 210=0, 210? can't, max=20<210. For n=909, splits: 909=81, 909=810, max=810<909. For n=1001, splits: 1001=1, 1001=10, 1001=100, max=100<1001. So yes.
Thus we just need to compute the number of iterations until a single-digit number is reached. Since Ni up to 1e6, but after first step, numbers could become up to? Maximum product for a d-digit number: For d digits, maximum product is achieved when we split roughly in half? But product is at most (10^{d-1} - 1) * (10^{1} - 1)? Actually worst-case product might be when both parts are as large as possible. For a d-digit number n, the maximum product is at most (10^{d-1} - 1)(10 - 1?) Let's compute upper bound: The left part a has length i (1 <= i <= d-1). Its maximum value is at most 10^{i} - 1 (if the prefix digits are all 9s). The right part b has length d-i, maximum value up to 10^{d-i} - 1. So product <= (10^{i} - 1)(10^{d-i} - 1). This is maximized when i = floor(d/2) or ceil(d/2). For d=6, max product roughly (10^3 -1)(10^3 -1) ~ 10^6, so could be close to 10^6. But actually n itself is <= 10^6 -1, so product might be close to n but less. For d=7 (max Ni is 1e6, which is 7 digits: 1,000,000 has 7 digits? Actually 1,000,000 is 7 digits? Wait 1e6 = 1,000,000, that is 7 digits. Ni <= 10^6, meaning up to and including 1,000,000? Usually constraints say 0 <= Ni <= 10^6, so maximum is 1,000,000 (7 digits). So product could be up to ~ (10^3 -1)(10^4 -1) = 999 * 9999 = about 9,989,001? But that's > 1e6. But can we get such a product? For n = 9999999? But n max is 1e6, which is 7 digits? Actually 1,000,000 is 7 digits, but numbers up to 1,000,000 are at most 7 digits, but the maximum product for a 7-digit number could be around (10^3 -1)(10^4 -1) = ~ 9999999 = 9,989,001, which is greater than 1,000,000. However, is it possible that a 7-digit number n yields a product that exceeds n? Let's test with n=1,000,000: splits: a=1, b=000000 => 0; a=10, b=00000 => 0; a=100, b=0000 => 0; a=1000, b=000 => 0; a=10000, b=00 => 0; a=100000, b=0 => 0; max=0 < n. So not. But consider n=999999? That's 6 digits, product max = 999999 = 998001 < 999999? Actually 999999=998001, yes less. For 7-digit numbers like 1234567? splits: 1234567=234567, 1234567 ~ 414804, 1234567 ~ 561,? 1234567=561,? Actually 1234567 = 561,? Let's compute: 4567100=456700, plus 234567=105041, total 561741. 1234567= 1234567 = 1234500=617000, 123467=82678, total 699678. 1234567= 1234560=740700, 123457=86415, total 827115. 1234567=864192. So max maybe 864192? All less than 1234567. But could any 7-digit number produce product > n? Let's try to see if inequality n > ab always holds. Our proof used n = a10^{k} + b, and we concluded n - ab = a(10^{k} - b) + b > 0 because a>0 and 10^{k} - b >= 1. This inequality holds regardless of d, as long as k >=1 and a>0. So indeed, for any split, product < n. So product can never exceed n. So max product < n. So f(n) < n. So it's decreasing. So even for numbers up to 10^6, product is less than n.
Thus, the sequence is strictly decreasing, so at most n steps. However, n can be up to 1e6, but we might need to compute the number of steps for each Ni efficiently. Since Q <= 100 and Ni <= 10^6, we could simply simulate the process for each Ni: while n >= 10, compute f(n) and increment count. Since each step reduces n, the number of steps is not too large. But is it possible that the number of steps could be large, like O(n) in worst case? For example, starting from 999999, the output says 12 steps. Starting from 1e6 maybe a few steps. So the number of steps is likely small because n reduces quite fast. However, to be safe, we should examine worst-case scenario. Let's try to see the worst-case number of steps for numbers up to 10^6. Could it be like 20 or 30? We can simulate for all numbers up to 10^6 to see maximum steps. But given the constraint, we can just simulate each query directly with a function that computes f(n). Since each step we need to compute max product over all splits of decimal representation. For a number with d digits, there are d-1 splits. d <= 7 initially, but after some steps, n might become numbers with many digits? Could n become larger than original? No, always decreasing. So digits d can only decrease or maybe stay same? For instance, 999 -> 891, 3 digits to 3 digits. Could digits increase? No, because product < n, so number of digits could sometimes stay the same but cannot increase. For example, 99 -> 81 (2 digits to 2 digits). 100 -> 0 (3 digits to 1 digit). So digits either reduce or remain same but number reduces. So maximum digits is at most the initial digits, which is at most 7. So computing f(n) each time is cheap: we can convert to string, loop over split positions, compute product and track max. So simulation per query is fine.
But wait: Could the process lead to numbers with more digits than initial? Consider n=10: product 0, which is 1 digit. So digits decrease. Could product have more digits? Suppose n=1000: product 0. So no. Could product be like 999? Starting from 9999? product max 9801, which is 4 digits, same as original. So digits can stay same. But can't exceed. So d <= initial d.
Thus simulation per query is O(steps * d^2?) Actually for each n we need to compute max product. We'll convert to string, length L. For each split i from 1 to L-1, we take left substring and right substring, convert to int, multiply, and take max. That's O(L^2) if we convert substrings each time, but L is at most 7, so trivial.
Thus we can compute for each Ni by simulation.
But we must confirm that steps are finite and never infinite. Since we proved f(n) < n, the loop will terminate. So we can implement.
But wait: There is a nuance: In step 2, they say "multiply the two obtained numbers and set the largest one as the next n". This means we consider all possible splits, compute product for each, and then choose the maximum product. That's what we did. And they also say: "You should also take into account those that have a 0 at the beginning of the digit as a result of the isolation." So we treat "024" as 24, which we did by converting to int. So that's fine.
Edge Cases: n = 0? Single digit, steps = 0. n = 1-9: steps = 0.
n = 10: while n>=10: n=10, splits: "1" and "0" -> 1*0=0. So f(10)=0, steps=1. Then 0 is single digit, stop. So steps=1.
n=1000000: compute f: For "1000000", length 7. Splits:
- 1 * 000000 = 0
- 10 * 00000 = 0
- 100 * 0000 = 0
- 1000 * 000 = 0
- 10000 * 00 = 0
- 100000 * 0 = 0
- also? Actually splits between each digit: positions 1-6. All products 0 because right part contains zeros only? For split after first: left=1, right=000000 -> int 0? Actually right "000000" as int = 0, product=0. After second: left="10", right="00000" -> product 10*0=0. etc. After last: left="1000000"? No, cannot split after last because right part would be empty. So all products zero. Max = 0. So next n=0. Steps =1. That matches the example: input 1000000 output 1. Good.
n=999999: Example output 12 steps. So simulation should yield that.
Thus solution: For each query, initialize steps=0. While n >= 10: compute f(n) as described, set n = f(n), steps++. Return steps.
We should also consider possible infinite loops? As argued, none. So -1 not needed. But to be safe, we can detect if we ever see the same number again (cycle) and return -1; but we can trust it won't happen.
Now, implement.
But before coding, let's verify some numbers manually to ensure we understand the process.
Example 1:
9 -> steps 0.
99 -> steps? Let's compute: 99: splits: 99=81; f=81; step1. 81: splits: 81=8? Wait 81: splits: 8 and 1 -> 8*1=8; f=8; step2. 8 is single digit. So steps=2. Output 2. Good.
123: we did steps=3. So matches.
Example 2:
999999 -> output 12. Let's test quickly to see if we get 12. We'll simulate later to confirm but trust.
Now, we should be careful: Could there be a case where the product is the same as n? Not possible as argued. So fine.
But is there any possibility that the process gets stuck in an infinite loop because the number becomes a single digit but step 2 is not applied? No.
Thus solution is straightforward.
But wait, there is a nuance: "If a certain integer n greater than or equal to 0 is a single digit in decimal notation, the process ends there." So 0 is a single digit (since 0 is a digit). So correct.
Now, just implement simulation.
However, note: For large Ni (like up to 10^6) the steps are small (max maybe 20). But we can test worst-case by exploring all numbers up to 10^6 to see max steps. Could be like 12 as per example for 999999. Let's try to think if any number yields more steps. Possibly numbers like 9...9 (many 9s) yield many steps. But 999999 yields 12. For 7-digit 9,999,999? But Ni <= 10^6, so max 1,000,000. So 999999 is max 6-digit. So max steps maybe 12. So trivial.
But what about 0? steps 0.
Thus, we can implement.
Implementation details:
- Read Q.
- For each Ni:
steps = 0
while Ni >= 10:
s = str(Ni)
max_prod = 0
for i in range(1, len(s)):
left = int(s[:i])
right = int(s[i:])
prod = left * right
if prod > max_prod: max_prod = prod
Ni = max_prod
steps += 1
print(steps)
Edge Cases: Ni could be 0. While loop condition Ni >= 10, so 0 skipped, steps 0.
Now, confirm with given examples.
Test 1:
9 -> while false, steps 0 => output 0.
99 -> first iteration: s='99', i=1: left=9, right=9, prod=81 => max_prod=81. Ni=81, steps=1. Next: 81 >=10, s='81', i=1: left=8, right=1, prod=8 => max_prod=8. Ni=8, steps=2. Now 8<10, exit, steps=2.
123: s='123', i=1: left=1, right=23=>23; i=2: left=12, right=3=>36; max=36. Ni=36 steps=1. 36: s='36', i=1: 36=18 => Ni=18 steps=2. 18: s='18', i=1: 18=8 => Ni=8 steps=3. exit => steps=3. Good.
Now test 999999:
We can simulate quickly to verify steps=12.
We'll code a test in mind: But it's okay.
Now, about -1: The problem says output -1 if infinite steps. Given proof, but maybe there is a subtlety: If n has leading zeros? But n is given normally without leading zeros. But after splitting, numbers with leading zeros are treated as numbers without leading zeros, so product could be less than n. So still decreasing. So no infinite steps.
However, one might think about n=0? Already single digit, steps=0. So no.
But wait, could there be a scenario where the product becomes a number that is again >=10 but the process repeats forever? Since strictly decreasing, it must hit a number <10 in at most n steps. Since n is finite, steps are finite. So infinite steps impossible.
Thus we never output -1. But perhaps the problem statement includes it as a general statement but in the given constraints it never happens. Or maybe there is a twist: If n = 10^k? Then f(n)=0, then stop. So all good.
Thus answer: compute steps as described.
But before finalizing, we should consider if there is any possibility that the process could get stuck because the product could be the same as n? Our inequality proof seems solid. Let's re-check: n = a * 10^k + b. a>0, 0 <= b < 10^k. Then product = ab. n - ab = a10^k + b - ab = a*(10^k - b) + b. Since 10^k > b (strictly because b < 10^k), 10^k - b >= 1. So a*(10^k - b) + b >= a1 + b > 0 because a > 0 or b >0? Actually a>0 ensures a(10^k - b) >0? If a>0 and (10^k - b)>=1, then a*(10^k - b) >=1. So the sum is at least 1 + b >= 1. So n > a*b strictly. So yes.
But what about case b=0? Then n - ab = a10^k + 0 - a0 = a10^k > 0. So product < n. Good.
What about case a could be zero? No, because a is a prefix of the decimal representation of n, and n has no leading zeros except n=0. But if n=0, it's single digit, we stop. So a>0.
Thus f(n) < n always.
Hence process terminates.
So answer: just simulate.
Now complexity: Q<=100, each simulation at most maybe 20 iterations, each iteration at most 7 splits, so trivial.
We should also consider that Ni might be up to 10^6, but after first step product could be up to? For 1e6, product=0, so steps=1. For 999999, steps=12. So fine.
Now, we must be careful: In Python, converting string to int for each split might be fine, but we can optimize if needed. Not necessary.
Thus solution is ready.
But perhaps there is a nuance: They say "multiply the two obtained numbers and set the largest one as the next n". They didn't say we consider all splits? Yes. So we compute all splits and take max product.
Thus code is straightforward.
I will produce a final answer with explanation.
But wait: There's a possible pitfall: For numbers like 100, we have splits: "1""00" = 10 =0, "10""0"=100=0. Max=0. So f(100)=0. So steps=1.
Now, confirm 999999 yields 12. Let's manually compute to ensure we understand process correctly.
We'll simulate 999999:
n=999999, d=6
Splits:
- i=1: 9*99999 = 899991
- i=2: 999999 = 989901? 999999 = 99*(10000-1)=990000-99=989901
- i=3: 999*999 = 998001
- i=4: 9999*99 = 989901 (same as i=2)
- i=5: 99999*9 = 899991 (same as i=1)
Max = 998001? Actually 998001 > 989901 > 899991, so max = 998001.
So f=998001. step=1
n=998001, digits=6
s='998001'
Splits:
i=1: 998001 = 882009
i=2: 998001 = 792099? 998001 = 998000+99=792000+99=792099
i=3: 998001 = 9981 = 998? Actually careful: right part "001" => int=1, product 9981=998.
i=4: 998001 = 99801=9980? Wait: "9980" as left, right "01" => int 1, product=9980.
i=5: 998001 = 99800
Compute all:
i=1: left=9, right=98001 -> 998001=882009
i=2: left=99, right=8001 -> 998001=792099
i=3: left=998, right=001 -> 9981=998
i=4: left=9980, right=01 -> 99801=9980
i=5: left=99800, right=1 -> 99800*1=99800
Max = 882009? Actually 882009 vs 792099 vs 998 vs 9980 vs 99800, so max is 882009.
But is 882009 > 792099? Yes. So f=882009. step=2
n=882009
s='882009'
i=1: 882009=656072? 882009=656072 (since 820098=656072)
i=2: 882009 = 882009 = 882000 + 889 = 176000 + 792 = 176792
i=3: 882009 = 8829=7938
i=4: 882009 = 88209=79380
i=5: 882009=793800
Max? 656072 vs 176792 vs 7938 vs 79380 vs 793800 => max = 793800? Actually 793800 > 656072? 793800 > 656072, yes. So max = 793800. step=3
n=793800
s='793800'
i=1: 793800 = 656600? 938007 = 656600
i=2: 793800 = 793800 = 300200? Actually 793800 = 7938100 = (7938)100, 7938=3002, so 300200? Let's compute: 793800 = 7938100 = (8038 - 38)100 = (3040-38)100 = 3002100 = 300200. Yes.
i=3: 793800 = 634400? 793800 = 634400.
i=4: 793800 = 0
i=5: 79380*0 = 0
Max among: 656600, 300200, 634400, 0, 0 => max = 656600? Actually compare 656600 vs 634400 vs 300200 => 656600 is highest. So f=656600. step=4
n=656600
s='656600'
i=1: 656600 = 339600
i=2: 656600 = 429000? 656600 = 6566100 = (4290)100? Actually 6566=4290, so 4290100 = 429000.
i=3: 656600 = 393600
i=4: 656600 = 0
i=5: 65660*0 = 0
Max = max(339600,429000,393600,0,0) = 429000. step=5
n=429000
s='429000'
i=1: 429000 = 116000
i=2: 429000 = 378000
i=3: 429000 = 0? Actually 4290=0
i=4: 429000 = 0
i=5: 429000 = 0
Max = 378000. step=6
n=378000
s='378000'
i=1: 378000 = 234000
i=2: 378000 = 296000
i=3: 378000 = 0
i=4: 378000 = 0
i=5: 37800*0 = 0
Max = 296000. step=7
n=296000
s='296000'
i=1: 296000 = 192000
i=2: 296000 = 174000? Actually 296000 = 174000.
i=3: 296000 = 0
i=4: 296000 = 0
i=5: 296000 = 0
Max = 192000. step=8
n=192000
s='192000'
i=1: 192000 = 92000
i=2: 192000 = 38000
i=3: 192000 = 0
i=4: 192000 = 0
i=5: 19200*0 = 0
Max = 92000. step=9
n=92000
s='92000' (5 digits)
i=1: 92000 = 18000
i=2: 92000 = 0? Actually 920=0
i=3: 92000 = 0
i=4: 9200*0 = 0
Max = 18000. step=10
n=18000
s='18000' (5 digits)
i=1: 18000 = 8000
i=2: 18000 = 0? 180=0
i=3: 18000 = 0
i=4: 1800*0 = 0
Max = 8000. step=11
n=8000
s='8000' (4 digits)
i=1: 8000 = 0? Actually 80=0
i=2: 8000 = 0
i=3: 8000 = 0
Max = 0. step=12
n=0 (single digit) stop.
Thus steps =12. So matches example.
So simulation yields correct.
Thus we can code.
Now, regarding -1: The problem statement says: "If you need an infinite number of steps, output -1." Since we proved none infinite, we never output -1. But to be safe, we could include a check for cycles: keep a set of visited numbers; if we revisit a number, then it's a cycle and would never reach single digit. Since all numbers are decreasing, we won't revisit. But maybe there is a case where f(n) could be equal to n? We argued not. So we can skip cycle detection. But if we want to be absolutely correct according to problem, we could add detection and output -1 if cycle detected. But given constraints and our reasoning, it's unnecessary.
However, I'll include a comment that due to monotonic decrease, termination is guaranteed, so no -1.
Thus final solution.
Now we write answer.
But note: The problem might be from some contest where they expected DP or memoization to compute steps for many queries quickly. But Q is small and Ni up to 10^6, simulation per query is fine. However, if we wanted to precompute for all numbers up to 10^6, we could also do DP, but not needed.
Let's test edge cases:
- n=10: steps=1
- n=11: 1*1=1, steps=1? Actually 11: f=1, then 1 is single digit, steps=1.
- n=1: steps=0.
- n=100: steps=1.
- n=101: f=10 (since splits: 101=1, 101=10) -> max=10, then 10->0, steps=2.
Now verify: For n=101, steps=2.
Now, confirm that step count is correct.
Now, what about numbers with many zeros like 1000, 10000? They go to 0 in 1 step.
Now, implement.
Potential pitfalls: When converting right substring to int, leading zeros are automatically handled, e.g., int("001") = 1. So that's fine.
We also need to consider that the product may be large? But maximum product for 7-digit numbers is less than (10^4-1)*(10^3-1) < 10^7, so within 32-bit int. So safe.
Thus solution code.
But before final answer, let's consider the possibility of infinite steps if we consider n=0? Already single digit.
Thus code:
def steps(n):
count=0
while n>=10:
s=str(n)
max_prod=0
for i in range(1, len(s)):
left=int(s[:i])
right=int(s[i:])
prod=left*right
if prod>max_prod:
max_prod=prod
n=max_prod
count+=1
return count
But note: The while condition n>=10. For n=0-9, count=0.
Now, check for n=10: s='10', i=1: left=1, right=0, prod=0. max_prod=0. n=0, count=1. While loop condition: after assignment n=0, loop condition checked at next iteration? Actually after count increment, we go to while condition: n>=10? 0>=10 false, so exit, count=1. So steps=1.
Now, what about n=0? while false, count=0.
All good.
Now, output each count.
Now, confirm example 2: 1000000 -> steps=1.
So code works.
Now, we must present solution in answer, with explanation and code.
But wait, the problem statement: "If you need an infinite number of steps, output -1." Since we determined it's never infinite, we could simply never output -1. But perhaps there is an oversight: Could there be a scenario where the process never reaches a single digit because it gets stuck at a number with two or more digits but f(n) = n? We proved that can't happen. So safe.
Thus answer: simulation.
But maybe they consider that the process ends when n is a single digit. If we get n=10, then step2 gives 0, which is a single digit, so steps=1. So yes.
Thus final answer.
But to be thorough, I'll include a check: if we ever encounter a number we have seen before, then it's a cycle and we return -1. But as argued, it won't happen. However, since the problem asks for it, we might implement that to be safe. It's simple: maintain a set visited, and before computing next n, if n in visited, return -1; else add n. But careful: For each Ni, we need to clear visited. Since numbers are decreasing, no cycle, but we can still do it.
Let's think if there could be a cycle if the numbers are not strictly decreasing? Our inequality shows product < n always, so it's strictly decreasing, so no cycle. So visited check unnecessary.
Thus, final solution: simulation.
Now, I'll produce final answer.
But before finishing, let's consider the possibility of n being negative? No, n>=0.
Now, I'll write the answer in the required format: for each Ni, output Ri.
Now, code in Python.
Potential optimization: If we precompute f(n) for all n up to 10^6, we could answer queries faster, but not needed.
Now, answer.
But also note: The input includes Q and then Ni lines. Output each Ri on separate line.
Edge Cases: Q could be 0? But constraints say Q >=1.
Now, finalize.
I'll now produce the final solution explanation and code. ```python
import sys
def step_count(initial: int) -> int:
n = initial
steps = 0
while n >= 10:
s = str(n)
max_prod = 0
# try all possible splits
for i in range(1, len(s)):
left = int(s[:i])
right = int(s[i:])
prod = left * right
if prod > max_prod:
max_prod = prod
n = max_prod
steps += 1
return steps
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
q = int(data[0])
out_lines = []
for i in range(1, q + 1):
ni = int(data[i])
out_lines.append(str(step_count(ni)))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()