We need to solve this problem. Let's restate: We have an integer n (1 ≤ n ≤ 10^9). For each operation, we replace every digit d of the current number with the decimal representation of d+1. Note that d+1 could be a single digit if d < 9, or two digits "10" if d = 9. So each digit transforms independently to either a string of length 1 (if d+1 ≤ 9) or length 2 (if d=9). After m operations, the resulting number is the concatenation of transformations of each digit after recursively applying operations? Wait careful: The operation is applied to the entire number. At each step, we take the current decimal representation (as a string) and for each digit d we replace it by the decimal string of d+1. That is a simultaneous transformation on all digits. After m operations, we get a string (the final number). We need its length modulo 1e9+7.
The number of digits can grow quickly because 9 becomes "10", which adds a digit. Then subsequent operations will act on those digits. So we need to compute, for each initial digit, the length of the string it expands to after m operations. Since digits transform independently (they don't interact across digit boundaries except concatenation), the total length is the sum over each initial digit of the length of the string resulting from starting with that digit and applying m operations. Because the transformation on the whole number is just applying the operation to each digit and concatenating, and the process is linear? Need to verify: Suppose we have a digit d, after one operation we get a string representing d+1. After two operations, we apply the operation to each digit of that string. So essentially, starting from a single digit, we apply m operations, and we get a string. The total length for initial number n is sum over its digits of f(d, m), where f(d,m) is the length of the expansion of digit d after m operations.
Thus the problem reduces to: For each digit d (0-9? but n can have digits 0? Actually n is integer, no leading zeros, but digits can be 0? Wait n is integer >= 1, so digits are 1-9 at most. However during operations, we can get digit 0 from transformation? Let's see: d+1 is always at least 1, and if d=9, we get "10" containing a digit 1 and 0. So digit 0 can appear. So we need to consider f for all digits 0-9. But initial digits are only 1-9, but we still need to handle 0 for recursion? Actually when we compute f for a digit, we will consider its evolution. For example, f(9,1)=2 (string "10"). For f(9,2), we start from digit 9, after 1 op -> "10". Then apply op: '1' becomes '2', '0' becomes '1'? Wait careful: d+1 for digit 0 gives 1? Because 0+1=1, so becomes "1". So after second op, "10" -> "2" from '1' and "1" from '0', so total "21"? Actually order: original digits: 1 and 0 -> transform: 1 -> 2 (single digit), 0 -> 1 (single digit). So the resulting string is "21". So f(9,2)=2 (since "21" length 2). Interesting, length can decrease? Actually after first op, length went from 1 to 2; after second op, length remained 2. So maybe it can increase or stay the same. Let's test: 9 -> 10 (len2). 10 -> 21 (len2). 21 -> 32 (len2) so stays 2 for subsequent? But if we have 8 -> 9 (len1) then -> 10 (len2) etc. Actually, we need to see behavior: For a digit <9, d+1 is a single digit, so after op, it stays a single digit, until maybe when that digit becomes 9? Let's simulate: start with digit 5, m=1 -> 6 (len1). m=2 -> 7 (len1). ... m=4 -> 9 (len1). m=5 -> 10 (len2). m=6 -> 21 (len2). m=7 -> 32 (len2). So length stays 2 from then onward? But after many operations, digits might become larger? Actually digits are always 0-9? The transformation: each digit d (0-9) is replaced by decimal representation of d+1. d+1 ranges from 1 to 10. So the representation is either one digit (if d+1 <=9) or two digits "10" (if d=9). So the resulting string consists of digits 1-9 and 0? Wait for d=8, 8+1=9 -> "9" (digit 9). For d=9, -> "10". For d=0, 0+1=1 -> "1". So after operation, all digits in the string are from 0 to 9. But note that 10 is two digits: 1 and 0. So after operation, we have only digits 0-9, no digits beyond 9. So the state of a digit is always a digit 0-9. So the evolution of a single digit expands into a string of digits, each in 0-9.
Thus f(d,m) is the total number of digits after m operations starting from a single digit d.
This is reminiscent of dynamic programming / linear recurrence. Since digits transform independently, we can treat this as: For each digit, after one operation, it becomes a string of digits. So f(d,m) = sum_{digit c in the expansion of d after one operation} f(c, m-1). But careful: After one operation, we have a string s = T(d), where T(d) is the decimal representation of d+1. Then after m-1 more operations, each digit in s evolves independently, and total length is sum of f(c, m-1) over c in s. So we have:
f(d, m) = Σ_{c ∈ digits of (d+1)} f(c, m-1)
Define T(d) as a list of digits: If d<9: [d+1] (single digit). If d==9: [1,0].
Thus base case: f(d, 0) = 1 (after 0 operations, just the digit itself, length 1). For m>0: f(d,m) = Σ_{c in next(d)} f(c, m-1).
We need to compute f(d,m) for d=0..9 and m up to 2e5. Since t up to 2e5 and sum of m across tests? Not exactly: each test has its own m, up to 2e5, and t up to 2e5, but we cannot compute per test naively. However we can precompute for all m up to max m among tests, for all digits 0-9. Then for each test, we sum over digits of n: f(d,m) mod MOD, and output that.
But careful: m can be up to 2e5, and t up to 2e5 => total sum of m can be huge (2e5 * 2e5 = 4e10) if we compute per test separately, but if we precompute once up to max m, then each test O(len(n)) ~ up to 10 (since n <= 1e9). So that's fine.
We need to compute f(d,m) for m from 0 to Mmax (max m across tests), for d=0..9. The recurrence is linear and has at most 10 states. But note the recurrence involves sums of f(c, m-1). This looks like we can compute using DP: dp[d][m] = length for digit d after m ops. Since Mmax <= 2e5, and 10*2e5 = 2e6, which is fine. However each dp step would require iterating over digits of T(d) which is at most 2, so O(10 * Mmax) operations, ~2e6, trivial.
But we need modulo 1e9+7. So we can compute DP for m=0..Mmax.
Wait: However, the recurrence is: f(d,m) = sum_{c in T(d)} f(c, m-1). So if we precompute for all m, we can just fill table.
But careful: The recurrence is based on m-1, so we can compute in increasing m order.
Thus algorithm:
-
Read all test cases. Keep track of max_m.
-
Precompute DP array dp[10][max_m+1] (or perhaps we can compute per m using arrays of size 10 for current and previous). But we need all m for answering each test (since each test may have different m). So either store all dp[d][m] for m from 0 to max_m. Since max_m up to 2e5, dp 10 * (2e5+1) ≈ 2e6 integers, OK memory (~16 MB). Or we could store only the final dp for each m? But we need sum over digits of n for given m, so we need dp[d][m] for each d. So we need to store dp for all m (or compute on the fly per test but that would be O(m) per test). So store.
-
Base: for all d, dp[d][0] = 1.
-
For m from 1 to max_m:
for d in 0..9:
if d != 9: dp[d][m] = dp[d+1][m-1] (since T(d) = [d+1])
else: dp[9][m] = (dp[1][m-1] + dp[0][m-1]) % MOD.
But careful: For d=9, T(9) = digits of 10: [1,0] => dp[9][m] = dp[1][m-1] + dp[0][m-1].
Also for d=0: T(0) = [1] => dp[0][m] = dp[1][m-1].
For d=1: T(1)=[2] => dp[1][m] = dp[2][m-1], etc.
Thus we can compute efficiently.
But there is a nuance: The recursion seems straightforward. Let's test for some values manually.
Compute dp[9][0]=1
dp[9][1] = dp[1][0] + dp[0][0] = 1+1=2. Good.
dp[9][2] = dp[1][1] + dp[0][1] = dp[2][0] + dp[1][0] = 1+1=2. Good.
dp[9][3] = dp[1][2] + dp[0][2] = dp[2][1] + dp[1][1] = dp[3][0] + dp[2][0] = 1+1=2. So stays 2 after m>=1? Actually dp[9][3] =2? Let's simulate: 9 ->10 ->21 ->32 (len2) Yes. So indeed for m>=1, dp[9][m] = 2? Let's compute dp[9][4] = dp[1][3]+dp[0][3] = dp[2][2]+dp[1][2] = dp[3][1]+dp[2][1] = dp[4][0]+dp[3][0] =1+1=2. So constant 2 for all m>=1. That seems plausible because after first operation we get "10", and subsequent operations on "10" produce strings of length 2 always (since digits are 1 and 0, each produce 1-digit strings? Let's see: 1->2, 0->1, so we always have two digits. And no further expansion because digits are 1 and 0; 1 stays as a digit until it becomes 9? Actually 1->2->3->... eventually 8->9->10? Wait if we have a digit 1, after many operations it will eventually become 9 and then 10, which would increase length. But note: In the string "10", we have digits 1 and 0. The digit 1 after many ops will become 9 and then produce "10", expanding length. Similarly digit 0 becomes 1, which may later become 9, etc. So maybe the length could increase beyond 2 after many operations? Let's simulate manually: Starting from "10" (len2). Apply operation: 1->2, 0->1 => "21" (len2). Next: 2->3, 1->2 => "32" (len2). Next: 3->4,2->3 => "43" (len2). Continue until one digit becomes 9? Let's see: Starting from "10", after k operations, the two digits will be (1+k) and (0+k) modulo? But digits are not modulo; they increase by 1 each operation. Actually careful: The operation replaces each digit d with decimal representation of d+1. So digits evolve independently. For a digit d, after t operations, it becomes not simply d+t, because when d becomes 9, it splits into two digits. But if we track the entire string, its length can change.
In our simulation, we started with digits 1 and 0. After each operation, they are both <9 until digit 1 becomes 9? Let's track:
State after m ops from start "10" (which is after 1 op from digit 9). Actually we have m from the perspective of the original digit, but let's just consider the evolution of "10" as an initial string.
Define string S0 = "10". Then operation 1: S1 = "21" (since 1->2, 0->1). S2 = "32". S3 = "43". S4 = "54". S5 = "65". S6 = "76". S7 = "87". S8 = "98". S9 = operation on "98": 9->10, 8->9 => "109"? Actually 9 becomes "10", 8 becomes "9", so concatenation "10"+"9" = "109". That's length 3! Wait check: Starting from "98": digits: 9 and 8. T(9)="10", T(8)="9". So resulting string is "10"+"9" = "109". So after 8 operations from initial "10", we got length 3. So our earlier claim that dp[9][m]=2 for all m>=1 is false. Let's compute with our recurrence carefully.
We computed dp[9][3] using recurrence: dp[9][3] = dp[1][2] + dp[0][2]. But dp[1][2] = dp[2][1] = dp[3][0] = 1? Wait let's compute properly:
We need to recalc with DP table systematically.
Define dp[d][m] as length for digit d after m operations.
Base: dp[d][0] = 1 for all d.
Recurrence for m>=1:
- If d != 9: dp[d][m] = dp[d+1][m-1]
- If d == 9: dp[9][m] = dp[1][m-1] + dp[0][m-1]
Let's compute small m manually:
m=0:
dp[*][0]=1.
m=1:
dp[0][1] = dp[1][0] = 1
dp[1][1] = dp[2][0] = 1
dp[2][1] = dp[3][0] = 1
...
dp[8][1] = dp[9][0] = 1
dp[9][1] = dp[1][0] + dp[0][0] = 1+1 = 2
m=2:
dp[0][2] = dp[1][1] = 1
dp[1][2] = dp[2][1] = 1
dp[2][2] = dp[3][1] = 1
dp[3][2] = dp[4][1] = 1
dp[4][2] = dp[5][1] = 1
dp[5][2] = dp[6][1] = 1
dp[6][2] = dp[7][1] = 1
dp[7][2] = dp[8][1] = 1
dp[8][2] = dp[9][1] = 2
dp[9][2] = dp[1][1] + dp[0][1] = 1 + 1 = 2
m=3:
dp[0][3] = dp[1][2] = 1
dp[1][3] = dp[2][2] = 1
dp[2][3] = dp[3][2] = 1
dp[3][3] = dp[4][2] = 1
dp[4][3] = dp[5][2] = 1
dp[5][3] = dp[6][2] = 1
dp[6][3] = dp[7][2] = 1
dp[7][3] = dp[8][2] = 2
dp[8][3] = dp[9][2] = 2
dp[9][3] = dp[1][2] + dp[0][2] = 1+1=2
m=4:
dp[0][4] = dp[1][3] = 1
dp[1][4] = dp[2][3] = 1
dp[2][4] = dp[3][3] = 1
dp[3][4] = dp[4][3] = 1
dp[4][4] = dp[5][3] = 1
dp[5][4] = dp[6][3] = 1
dp[6][4] = dp[7][3] = 2
dp[7][4] = dp[8][3] = 2
dp[8][4] = dp[9][3] = 2
dp[9][4] = dp[1][3] + dp[0][3] = 1+1=2
We see dp[9][4] = 2. But earlier we argued that after 8 operations from "10", length becomes 3. Let's map that: Starting from digit 9, after m operations we have string length dp[9][m]. After 1 op (m=1) we have "10" length 2. After 2 ops: "21" length 2. After 3: "32" length 2. After 4: "43"? Wait according to dp[9][4]=2, that would be length 2. But from our manual step: from "10" (which is after 1 op), after another op we get "21" (m=2). After third: "32" (m=3). After fourth: "43" (m=4). After fifth: "54" (m=5). After sixth: "65" (m=6). After seventh: "76" (m=7). After eighth: "87" (m=8). After ninth: "98" -> wait that's m=9? Let's list m from start of 9:
m=0: "9" (len1)
m=1: "10" (len2)
m=2: "21" (len2)
m=3: "32" (len2)
m=4: "43" (len2)
m=5: "54" (len2)
m=6: "65" (len2)
m=7: "76" (len2)
m=8: "87" (len2)
m=9: "98" (len2) ??? But earlier we thought after 8 steps from "10" we get "98"? Let's recalc properly:
We have initial digit 9.
Operation 1 (m=1): T(9) = "10" -> string "10"
Operation 2 (m=2): apply to "10": 1->2, 0->1 => "21"
Operation 3 (m=3): 2->3, 1->2 => "32"
Operation 4 (m=4): 3->4, 2->3 => "43"
Operation 5 (m=5): 4->5, 3->4 => "54"
Operation 6 (m=6): 5->6, 4->5 => "65"
Operation 7 (m=7): 6->7, 5->6 => "76"
Operation 8 (m=8): 7->8, 6->7 => "87"
Operation 9 (m=9): 8->9, 7->8 => "98"? Wait careful: At m=8 we have "87". So after operation 9, we transform digits 8 and 7. 8->9 (single), 7->8 (single) => "98". That's length 2.
Operation 10 (m=10): transform "98": 9->10, 8->9 => "109" length 3.
So dp[9][10] should be 3. But our recurrence gave dp[9][4]=2, etc. But we haven't computed m=10. Let's compute using recurrence for m=10. We'll need dp values.
So our recurrence suggests that dp[9][m] is not constant 2; it will eventually increase as m grows. But from our earlier computation of dp[9][4], it's 2. Let's compute further using the recurrence to see if it grows.
We can compute a few more m:
We have dp table for m up to 4. Continue:
m=5:
dp[0][5] = dp[1][4] = 1
dp[1][5] = dp[2][4] = 1
dp[2][5] = dp[3][4] = 1
dp[3][5] = dp[4][4] = 1
dp[4][5] = dp[5][4] = 1
dp[5][5] = dp[6][4] = 2? Wait dp[6][4] we computed as 2. Yes dp[6][4]=2. So dp[5][5] = 2
dp[6][5] = dp[7][4] = 2
dp[7][5] = dp[8][4] = 2
dp[8][5] = dp[9][4] = 2
dp[9][5] = dp[1][4] + dp[0][4] = 1+1=2
m=6:
dp[0][6] = dp[1][5] = 1
dp[1][6] = dp[2][5] = 1
dp[2][6] = dp[3][5] = 1
dp[3][6] = dp[4][5] = 1
dp[4][6] = dp[5][5] = 2
dp[5][6] = dp[6][5] = 2
dp[6][6] = dp[7][5] = 2
dp[7][6] = dp[8][5] = 2
dp[8][6] = dp[9][5] = 2
dp[9][6] = dp[1][5] + dp[0][5] = 1+1=2
m=7:
dp[0][7] = dp[1][6] = 1
dp[1][7] = dp[2][6] = 1
dp[2][7] = dp[3][6] = 1
dp[3][7] = dp[4][6] = 2
dp[4][7] = dp[5][6] = 2
dp[5][7] = dp[6][6] = 2
dp[6][7] = dp[7][6] = 2
dp[7][7] = dp[8][6] = 2
dp[8][7] = dp[9][6] = 2
dp[9][7] = dp[1][6] + dp[0][6] = 1+1=2
m=8:
dp[0][8] = dp[1][7] = 1
dp[1][8] = dp[2][7] = 1
dp[2][8] = dp[3][7] = 2? Wait dp[3][7]=2, so dp[2][8] = 2
dp[3][8] = dp[4][7] = 2
dp[4][8] = dp[5][7] = 2
dp[5][8] = dp[6][7] = 2
dp[6][8] = dp[7][7] = 2
dp[7][8] = dp[8][7] = 2
dp[8][8] = dp[9][7] = 2
dp[9][8] = dp[1][7] + dp[0][7] = 1+1=2
m=9:
dp[0][9] = dp[1][8] = 1
dp[1][9] = dp[2][8] = 2
dp[2][9] = dp[3][8] = 2
dp[3][9] = dp[4][8] = 2
dp[4][9] = dp[5][8] = 2
dp[5][9] = dp[6][8] = 2
dp[6][9] = dp[7][8] = 2
dp[7][9] = dp[8][8] = 2
dp[8][9] = dp[9][8] = 2
dp[9][9] = dp[1][8] + dp[0][8] = 1+1=2
m=10:
dp[0][10] = dp[1][9] = 2
dp[1][10] = dp[2][9] = 2
dp[2][10] = dp[3][9] = 2
dp[3][10] = dp[4][9] = 2
dp[4][10] = dp[5][9] = 2
dp[5][10] = dp[6][9] = 2
dp[6][10] = dp[7][9] = 2
dp[7][10] = dp[8][9] = 2
dp[8][10] = dp[9][9] = 2
dp[9][10] = dp[1][9] + dp[0][9] = 2 + 1 = 3
Yes, dp[9][10] = 3. So indeed dp[9][m] increases to 3 at m=10. So our DP works.
Thus we can precompute up to max m.
But note: max m across test cases can be up to 2e5. That's fine.
But we need to handle t up to 2e5 and n up to 1e9 (so at most 10 digits). So precomputation O(10 * Mmax) ~ 2e6, then each test O(log10(n)). This is trivial.
However, we must be careful with memory: We need dp[10][Mmax+1] but Mmax up to 2e5, so that's 2e6 integers. Each integer maybe 4 bytes, so 8 MB, fine.
Alternatively, we could use DP with two arrays of length 10 and compute values for each m and store in a vector of length Mmax+1 for each digit? But we need to retrieve f(d,m) for arbitrary m. So we could compute a list for each d? Actually we need to answer queries (d,m). Since d is only 10, we could precompute an array ans_d[m] for each d. So dp[d][m] works.
Edge Cases: Digits 0 may appear as initial? n is positive integer, so its digits are from 1-9 only. But we compute for d=0 for completeness anyway. Since n has no leading zeros, we don't sum dp[0][m] from the original number. But that's fine.
Now check sample:
Test 1: n=1912, m=1. digits: 1,9,1,2. Compute dp[1][1]=? dp[1][1]=1. dp[9][1]=2. dp[1][1]=1. dp[2][1]=1. Sum=1+2+1+1=5. Output 5. Matches.
Test2: n=5, m=6. dp[5][6]? From our computed dp[5][6]=2 (see m=6 row: dp[5][6]=2). So output 2. Matches.
Test3: n=999, m=1. digits three 9s: each dp[9][1]=2 => total 6. Matches.
Test4: n=88, m=2. digits: 8,8. dp[8][2]? From m=2: dp[8][2]=2. So sum=2+2=4. Matches.
Test5: n=12, m=100. Need dp[1][100] + dp[2][100]. We'll compute later. Output expected 2115.
So DP works.
But maybe we can find closed form or matrix exponentiation? Not necessary given constraints.
But there is a nuance: The DP recurrence as defined uses dp[d][m] = dp[d+1][m-1] for d<9. That's simple. So we can compute dp array quickly.
One approach: we can treat it as a linear recurrence that can be computed via iterative updating. But careful: dp[d][m] depends on dp[d+1][m-1], so to compute dp for all d for a given m, we need previous m-1 values. So we can iterate m from 1 to Mmax, and compute each dp[d][m] using stored dp[·][m-1].
Implementation:
- Read t, store test cases to know max_m.
- Precompute dp[10][max_m+1].
- For m=0: dp[d][0]=1 for all d.
- For m from 1 to max_m:
for d from 0 to 8: dp[d][m] = dp[d+1][m-1] % MOD.
dp[9][m] = (dp[1][m-1] + dp[0][m-1]) % MOD.
That's it.
But is this correct for d=0? d=0 -> T(0) = "1" => dp[0][m] = dp[1][m-1]. Our loop sets dp[0][m] = dp[1][m-1] because 0 is <9, so that's fine. For d=8: dp[8][m] = dp[9][m-1] which is correct.
Now, compute answer for each test case: parse n as string, for each digit d (0-9 char) convert to integer, then ans = (ans + dp[d][m]) % MOD. Output.
Complexities: O(t * digits + 10*max_m).
But wait, max_m up to 2e5, t up to 2e5, product t * digits ~ 2e6, fine.
Memory: dp as 10 x (max_m+1) integers, each 32-bit. That's fine.
Now, confirm with test 5: n=12, m=100. We'll compute dp[1][100] and dp[2][100] after precomputing. Let's quickly simulate programmatically to verify expected 2115. But trust.
Now consider whether we can do better? Not needed.
But there is a nuance: The DP recurrence may be expressed in a way that we only need to store two arrays of size 10 and compute all m on the fly, and then store the values for each m? But we need to answer queries for many m. If we only keep dp for each m as we compute, we need to record the values for each digit for each m? Actually for each test we need f(d,m) for specific m, not for all d? But we need for each digit of n. So we need the value for each d for that m. If we compute in increasing m and store an array of length (max_m+1) for each digit, that's fine.
Alternative: Since d only 10, we could precompute a table where for each m, we have an array of 10 values, or we could compute on the fly for each test by simulating using DP but that would be O(m) per test, too slow.
So precomputation is best.
But one more nuance: The maximum m across test cases is 2e5, but there could be many test cases with large m. So precomputation O(2e6) is fine.
Now, think about the potential for larger constraints? Already within.
But is there any subtlety regarding the recurrence? Let's verify for d=9: dp[9][m] = dp[1][m-1] + dp[0][m-1]. Indeed T(9) = "10" => digits 1 and 0. So it's correct.
Now, we need to ensure that when we compute dp[0][m] we treat correctly: dp[0][m] = dp[1][m-1]. That matches T(0) = "1".
Now, let's test dp[0][1] = dp[1][0] = 1, which means starting from digit 0, after 1 op, we get "1", length 1. That's correct: 0+1 =1 -> "1". dp[0][2] = dp[1][1] = 1, so after 2 ops: from 0 -> 1 after first op, then ->2 after second? Actually: start 0, op1-> "1" (len1), op2: digit 1 -> "2" (len1). So length 1. Good.
Now, test a few more manually: dp[8][3] we computed as 2. Let's simulate: start 8. op1: 9 -> "9"? Wait 8+1=9 -> "9", length 1. op2: 9 -> "10", length 2. op3: "10" -> "21", length 2. So dp[8][3] should be 2. OK.
dp[7][4] we computed as 2. Let's simulate: 7->8->9->10->21? Actually start 7. m=1: 8 -> "8" len1. m=2: 9 -> "9" len1. m=3: 10 -> "10" len2. m=4: "21" len2. So dp[7][4]=2. Good.
Now, confirm dp[6][5] =2? Start 6: 1:7, 2:8, 3:9, 4:10, 5:21 -> len2. So correct.
So DP seems consistent.
Now, we must be careful about modulo: Use MOD = 1e9+7.
Now, is there any other subtlety? Possibly large m up to 2e5, but dp values can be large, but modulo reduces.
But wait, are we sure that digits evolve independently? Yes because operation is applied digit-wise: each digit is replaced by a fixed string that depends only on that digit, and the order of concatenation is preserved. So the total length after m operations is sum of lengths contributed by each original digit. There's no cross-digit interference. So it's additive.
Thus solution is straightforward.
But we should confirm the sample test 5: n=12, m=100 => expected 2115. Let's simulate quickly with code later.
Now, think about optimization: Since t up to 2e5, reading and processing each test case with string conversion is fine.
Now, I'll think about possible pitfalls: The integer n can be up to 1e9, so at most 10 digits, so summing up to 10 values mod MOD is trivial.
Now, maybe we need to consider leading zeros? n is given as integer; no leading zeros. So okay.
Now, consider that m can be 0? Input says m (1 ≤ m ≤ 2e5). So m >= 1, but we still need base case m=0 for dp? Yes for DP we need m=0 as base. But we will compute up to max_m and m>=1 in queries. So fine.
Now, test with m=200000 and digits, dp values can be huge modulo.
Now, we need to ensure we use appropriate data types (int64 perhaps for intermediate sum). Use long long in C++ or Python.
Now, implement.
But wait: Is the DP recurrence correct for all digits? Let's check digit 0: T(0)="1", length 1, but careful: Digit 0 is not in initial number but appears later. For digit 0, after operation, we get "1". So dp[0][m] = dp[1][m-1] is correct.
Now, check digit 1: T(1)="2", dp[1][m] = dp[2][m-1].
Now, digit 8: T(8)="9", dp[8][m] = dp[9][m-1].
Now, digit 9: T(9)="10", dp[9][m] = dp[1][m-1] + dp[0][m-1].
Thus we can compute.
Now, maybe there is a more efficient way using matrix exponentiation because m up to 2e5 is fine. But DP is simpler.
But wait: t can be up to 2e5 and max_m also 2e5, so DP table 2e6 entries, each maybe 4 bytes, 8 MB, plus test cases storage, fine.
Now, confirm with sample 5: Let's quickly compute dp[1][100] and dp[2][100] manually via code logic? Let's reason about recurrence to see if we can compute by hand? Not needed.
I will implement a quick Python script to compute dp up to 100 and sum for digits 1 and 2 to verify it's 2115.
But as ChatGPT, I can't run code, but I can simulate conceptually.
Given dp recurrence, we can try to compute small values and see pattern. But it's fine.
Thus solution plan:
-
Read t.
-
For each test, read n and m. Store in list to find max_m, or we can precompute up to max_m on the fly: we could sort test cases by m and compute incrementally, but simpler: read all, store (n, m) and track max_m, then precompute dp up to max_m.
-
Precompute dp as 2D list: dp[10][max_m+1] with 0-index for m.
-
Base: for d in 0..9: dp[d][0] = 1
-
For i from 1 to max_m:
for d in 0..8: dp[d][i] = dp[d+1][i-1]
dp[9][i] = (dp[0][i-1] + dp[1][i-1]) % MOD
Alternatively we can compute in one pass.
- Then for each test case: convert n to string, sum dp[int(c)][m] % MOD. Print.
Now, one more nuance: The operations are applied simultaneously to all digits. The DP we defined is for a single digit. Since digits transform independently, the total length is sum of lengths of each digit's evolution. This is correct because the operation on the whole number is essentially applying to each digit and concatenating results. And subsequent operations apply to each digit in the resulting string, preserving the independent evolution because there's no mixing: each digit's evolution only depends on its own starting digit and number of ops. However, is there any interaction due to concatenation? For example, when a digit becomes "10", that's two digits. In the next operation, each of those two digits is transformed independently. This is exactly captured by our DP: f(d,m) = sum over digits of T(d) of f(c, m-1). So additive property holds. So total length = sum f(initial_digit, m). So it's correct.
Now, think about possible overflow: We store modulo. Since final answer modulo 1e9+7, we can compute modulo.
Now, complexity: O(10 * max_m + total_digits). With max_m=2e5, this is ~2e6 operations, which is trivial for Python? 2e6 is fine, but t up to 2e5 may cause I/O overhead; need to use sys.stdin.read for speed.
In Python, 2e6 operations is fine, but we also have to compute dp for 2e5 m values, each with 10 assignments, so 2 million assignments, plus maybe some loops, that's fine. But note Python can handle up to 1e7 simple ops, so fine.
But I recall that Codeforces typical constraints for Python require efficient I/O. We'll implement using sys.stdin.buffer.read and parse.
Now, careful: We need to handle up to 2e5 test cases, each with n up to 1e9 and m up to 2e5. The total input size is about 2e5 * (some digits) ~ few MB, fine.
But we should be careful with memory for storing all test cases: we could process on the fly after precomputation if we know max_m first. We can first read all input to get max_m. But we could also precompute up to max_m in one pass while reading? But we need max_m before precomputation. So either read all lines, store (n,m) and track max_m, or two-pass: first read all lines to get max_m, then precompute, then process stored data.
Given t up to 2e5, storing pairs is okay: store n as string maybe and m as int. That's about 2e5 * (10 bytes for n + 4 bytes) ~ a few MB, fine.
Alternatively, we could read all, find max_m, precompute, then re-iterate through stored data. Or we could sort by m but not needed.
I'll store test cases as list of (n_str, m). Or we can read, store n as string for later conversion to digits.
Now, implement in Python:
import sys
MOD = 10**9+7
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
t = int(next(it))
tests = []
max_m = 0
for _ in range(t):
n_str = next(it).decode()
m = int(next(it))
tests.append((n_str, m))
if m > max_m:
max_m = m
# precompute dp[10][max_m+1]
# Use list of lists: dp[d][m]
dp = [[0]*(max_m+1) for _ in range(10)]
for d in range(10):
dp[d][0] = 1
for i in range(1, max_m+1):
# for d 0..8
for d in range(9):
dp[d][i] = dp[d+1][i-1] % MOD # already mod but small
dp[9][i] = (dp[0][i-1] + dp[1][i-1]) % MOD
out = []
for n_str, m in tests:
total = 0
for ch in n_str:
d = ord(ch) - 48
total = (total + dp[d][m]) % MOD
out.append(str(total))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
main()
But note: The DP recurrence we wrote uses dp[d][i] = dp[d+1][i-1] for d in 0..8. However, dp[d][i] will be computed mod MOD, but dp values are integers anyway.
One could also compute dp as two rows for each m? But we stored full.
Potential optimization: The DP for d<9 uses the value from d+1 at previous m, which may be computed earlier in the same i iteration? Since we are iterating d from 0 to 8, we are using dp[d+1][i-1] which is from previous i, so it's safe. No dependency within same i.
Now, check with sample. Should output correct.
But I'm concerned about dp[0][i] being computed as dp[1][i-1]. But in the recurrence, for d=0, it should be dp[1][i-1]. That's correct.
Now, we need to ensure that dp[9][i] uses dp[0][i-1] and dp[1][i-1]. Those are already computed for i-1.
All good.
Now, time complexity: O(max_m*10 + sum_digits). With max_m=2e5, that's 2e6 operations. Python can handle that easily.
But there is a nuance: The test case "12 100" output 2115. Let's quickly compute using our DP concept to confirm.
We can try to compute dp[1][100] and dp[2][100] manually with a small script mentally? Not necessary; trust.
Now, consider potential large memory: dp as list of 10 lists each length up to 200001, each int 28 bytes in Python? Actually Python ints are objects (28 bytes each), so 10*200001 ~ 2 million ints, each ~28 bytes => 56 MB, maybe more. That could be high but might still pass within memory limit (typically 256 MB). But 2 million Python ints could be ~56 MB, plus overhead for list structures (each inner list has overhead). Actually each inner list holds references to Python int objects. The ints themselves are separate objects, but small integers are cached? Actually Python caches small integers from -5 to 256, but our dp values will become large beyond 256 quickly. So each dp entry will be a new int object. So memory might be around 2 million * 28 = 56 MB for ints plus list overhead (each list holds 200001 references, 8 bytes each reference? Actually Python list holds pointers, 8 bytes each). So total memory for dp: 10 lists * 200001 * 8 bytes = ~16 MB for references, plus int objects for each distinct value. But each int object ~28 bytes. So total ~16 + 56 = 72 MB. Plus overhead for lists themselves, maybe 80 MB. That might be okay but could be near limit if memory limit is 256 MB. However, in Codeforces Python memory limit is often 256 MB, so 80 MB is fine. But we also store tests: up to 2e5 test cases, each storing a string (n) and an int. That's additional memory. For each test, we store n_str as bytes? In our code we decode to string; storing 2e5 strings of up to 10 characters each might be ~2e6 bytes plus overhead of Python objects, which could be large. But we could avoid storing all tests by processing after precomputation if we could first read all to get max_m, then precompute, then process again? But we need to read input only once; we can store test cases in a list of (n_str, m). For 2e5 entries, each entry is a tuple of a string and an int. That's also memory heavy: each tuple ~56 bytes, plus string objects. Could be tens of MB. But still likely under 256 MB? Let's approximate: 2e5 tuples ~ 2e5 * 56 = ~11 MB; strings: each up to 10 chars, but Python stores each string with overhead, say 49 bytes + length, so maybe 60 bytes each => 12 MB; ints: each int maybe 28 bytes => 5.6 MB. So total ~30 MB. Plus dp ~80 MB => ~110 MB. Might be okay. But we can reduce memory by not storing all test cases: we can read all lines into memory, parse to get max_m, then precompute, then iterate again? But we cannot re-read input. So we need to store them. Alternatively, we could read all input, split, and process sequentially while tracking max_m? But we need max_m to precompute before we can answer queries. So we need to know max_m before computing answers. We could read all test cases into a list, find max_m, precompute dp, then answer. That's fine.
But we can also compute dp incrementally for each m up to the needed m for each test if we sort tests by m. That would avoid storing all? We would still need to store tests to output in order. But we could read all, store, sort by m, then compute dp up to current max as we go, and then reorder answers. That might be more efficient memory? Actually we still need to store all tests. So same.
Given typical CF constraints, Python solutions with DP table and storing tests pass. Many solutions to this problem used DP with array of size 10 for each m? Actually I recall that many solutions used a DP of length 10 and computed for each test individually? But t up to 2e5, each m up to 2e5 would be too slow. So they precompute.
But I've seen solution: Use dp array of size 10, but precompute for each m the transformation of digits? Wait they often compute a DP where for each digit we know the length after m operations, but they might use matrix exponentiation or precompute for all m up to 2e5? They often have DP[10][max_m+1] like we do.
Now, we might try to reduce memory by only storing dp for each digit as a list of ints, which we already do.
But 2 million ints in Python might be borderline but likely OK. Actually I've used similar DP in Python with 2 million entries and it passed.
Alternatively, we can note that the recurrence is linear and we can compute f(d,m) using DP that only depends on previous m, but we need to answer many m, so we need to store the values for each m for each digit? We could store only for each digit a list of length max_m+1. That's what we have.
Now, think about optimizing memory: Since we only need dp[d][m] for the specific m of each test, we could compute dp for all m up to max_m, but store only the values for each m? That is the same.
Alternatively, we could compute a single array for each digit of length max_m+1. That's what we have.
Maybe we can compress by noticing that we only need to answer sum over digits, but digits are only 10, so we could precompute for each m the sum over digits? Not exactly, because the digits of n vary per test. So we need per digit values.
Now, I'll think about an alternative: The recurrence can be expressed as linear transformations and we can precompute using exponentiation? Not needed.
Thus, the solution is implemented.
Now, we should double-check the recurrence for d=0..8: dp[d][m] = dp[d+1][m-1]. This means that for any m, the value for d is essentially dp[d+1][m-1], which eventually becomes something like dp[9][m-?]. Actually we can reduce to dp[9] for large differences. But it's fine.
Now, we must confirm that our DP correctly handles m up to 2e5. The values can become large (exponential-like) but modulo 1e9+7.
Now, check sample 5 with m=100. Let's quickly compute small values to see pattern, but not necessary.
Now, consider possible pitfalls: The operation count m may be 0? Input says m >= 1, but we have base case for m=0 anyway for DP.
Now, ensure we handle modulo for dp[9][i] as sum of two possibly large numbers.
Now, edge case: n might be large, but only up to 1e9, so digits at most 10.
Now, implement.
But before finalizing, I want to verify that the DP recurrence is indeed correct after many operations. Let's test with a small example: d=8, m=10 using DP from earlier. From our DP we have:
We computed up to m=10. Let's compute dp[8][10] from table: m=10, d=8 = dp[9][9] = 2? Actually dp[8][10] = dp[9][9] from recurrence? Wait: dp[8][i] = dp[9][i-1]? Yes for d=8, dp[8][i] = dp[9][i-1]. So dp[8][10] = dp[9][9]. From earlier we had dp[9][9] = 2. So dp[8][10] = 2.
Let's simulate manually: start 8.
- m=0: "8" len1
- m=1: "9" len1
- m=2: "10" len2
- m=3: "21" len2
- m=4: "32" len2
- m=5: "43" len2
- m=6: "54" len2
- m=7: "65" len2
- m=8: "76" len2
- m=9: "87" len2
- m=10: "98" len2? Wait earlier we said at m=10 from start 8? Let's recalc: Start with digit 8.
Operations:
0: 8
1: 9 (since 8+1=9)
2: 10 (9+1=10)
3: 21 (1->2, 0->1)
4: 32
5: 43
6: 54
7: 65
8: 76
9: 87
10: 98 (8->9, 7->8) Actually after m=9 we had "87"? Let's step carefully:
Let s0 = "8"
m=1: apply to s0: 8->9 => "9"
m=2: apply to "9": 9->10 => "10"
m=3: apply to "10": 1->2, 0->1 => "21"
m=4: apply to "21": 2->3, 1->2 => "32"
m=5: apply to "32": 3->4, 2->3 => "43"
m=6: apply to "43": 4->5, 3->4 => "54"
m=7: apply to "54": 5->6, 4->5 => "65"
m=8: apply to "65": 6->7, 5->6 => "76"
m=9: apply to "76": 7->8, 6->7 => "87"
m=10: apply to "87": 8->9, 7->8 => "98"
"98" length