We are asked: for each t up to 5000, each ni up to 1e18, we need to represent ni as sum of exactly six lucky numbers (lucky digits: 0,4,7). Lucky numbers are non-negative integers whose decimal representation contains only digits 0,4,7. For example 0, 47, 7074 are lucky. So we can use 0 as a lucky number.
We need to output six lucky numbers (presumably in any order) that sum to ni, or -1 if impossible.
Constraints: t up to 5000, ni up to 1e18. We need an efficient method per test case, perhaps with a constructive algorithm.
First, think: what numbers can be expressed as sum of six lucky numbers? Since lucky numbers can be large and contain digits only 0,4,7, they are quite sparse but we have zero, and we can use any combination. The problem may be always possible except for some small numbers. But we need to verify.
Given we have six numbers, we can always add zeros to fill up to six numbers if we use fewer. But we must have exactly six numbers. So we can always include extra zeros if needed. So the key is whether ni can be expressed as sum of at most six lucky numbers (with zeros to fill). Since zeros are allowed, we need to represent ni as sum of up to six lucky numbers, but exactly six if we include zeros.
Thus we need to decide if any number can be expressed as sum of at most six lucky numbers. This reminds of the concept of representing any integer as sum of a fixed number of numbers from a set that includes 0 and enough flexibility.
Given digits only 4 and 7 (plus 0), the lucky numbers are of the form sum_{k} digit_k * 10^k where digit_k in {0,4,7}. So each decimal place can be 0,4,7 times the place value. When we add six such numbers, the total sum's digits (in base 10) are the sum of the six digits per column (with carries). So effectively, we need to find for each digit position, the sum of six digits each in {0,4,7} plus carry from lower positions, equals the target digit at that position (mod 10), and the carry propagates.
This becomes a digit DP problem: for each decimal position we need to assign six digits (each 0,4,7) such that total sum matches. Since we have only six numbers, each digit position's sum of six digits ranges from 0 to 6*7=42. With carries, it's more.
But we need a constructive algorithm for up to 1e18, maybe we can always represent any integer >= something with six lucky numbers, and handle small ones separately.
Let's try to find if there are any impossible numbers. Let's think about modular constraints. Each lucky number mod something? For example, consider modulo 9: digit sum of a number mod 9 is congruent to number mod 9. For lucky numbers, digits are only 0,4,7, so the sum of digits mod something? Actually, number mod 3? But not sure.
But note we have zeros, so we can add zeros to adjust the count but not sum.
We need exactly six numbers. Could we always use many zeros? zeros help. But we need sum exactly ni. So essentially we need to find up to six lucky numbers (including possibly zeros) that sum to ni. Since zeros are lucky, we can always use fewer than six by adding extra zeros. So we need to see if every positive integer can be expressed as sum of at most 5 lucky numbers? Not exactly, because we have exactly six, but we can use zeros.
Thus we need to find for each ni, six lucky numbers a1,...,a6 such that sum = ni. Equivalent to: there exist lucky numbers b1,...,bk (k <=6) summing to ni, then set the rest to zeros.
So the existence question reduces to: can ni be expressed as sum of at most six lucky numbers? Because we can fill the rest with zeros.
Now, are there numbers that cannot be expressed as sum of at most six lucky numbers? Possibly some small numbers.
Let's test small numbers manually.
Lucky numbers (positive) start: 0,4,7,40,44,47,70,74,77,400,404,407,440,444,447,470,474,477,700,... etc. They can be large.
Since we can use large numbers, perhaps we can represent any integer >= some threshold. Since we have six numbers, we can break down the decimal digits.
But consider number 1: cannot be expressed using only digits 0,4,7 in sum of six numbers. The minimum positive sum we can get with six non-zero lucky numbers is using six 4's? But we can use zeros: actually we can use some zeros and some lucky numbers. The smallest positive lucky number is 4. So the smallest sum we can get from at most six lucky numbers (with at least one positive) is 4 (if we use one 4 and rest zeros). So numbers 1,2,3 cannot be expressed. What about 5? Could 5 be expressed as sum of lucky numbers? 4+? Next smallest is 0,4,7. 4+0=4; 4+4=8; 4+7=11; 7+7=14; etc. So 5 not possible. 6? 4+? no; 7+? 7+? need -1. So not possible. 8? 4+4=8 yes (using two numbers, zeros for rest). So 8 is possible. 9? 4+4+? need 1 impossible; 7+? need 2 impossible; 4+7=11; so 9? 4+4+? Actually 9 could be 4+4+? but minimum extra is 0, sum=8; need 1 impossible. Could be 7+? need 2 impossible. So 9 not possible? What about using three numbers: 4+4+4=12, too high. So 9 not possible. 10? 4+?6 impossible; 7+?3 impossible; 4+4+?2 impossible; 4+7=11; so 10? maybe 4+?6 no; 7+?3 no; maybe 0? No. Could 10 be 4+? Actually 4+4+?2 no; 7+?3 no; 4+4+4=12; so 10 not possible. 11: 4+7=11, yes. 12: 4+4+4=12. 13: 4+4+?5 no; 7+?6 no; 4+7+?2 no; 4+4+7=15; 7+7=14; so 13? maybe 4+4+4+?1 no; 4+4+7=15; 7+7=14; 13 not possible? Could be 4+4+?5 no. So 13 not possible. 14: 7+7=14. 15: 4+4+7=15. 16: 4+4+4+4=16. 17: 4+4+4+?5 no; 7+7+?3 no; 4+7+?6 no; 4+4+4+4+?1 no; 4+4+7+?2 no; 7+7+?3 no; 4+4+4+7=19; 7+7+4=18; so 17 seems impossible? But example input: 17 yields -1. So indeed 17 is impossible. Good.
So small numbers may be impossible.
But we need to handle up to 1e18. Likely the answer is always possible except for some small set. Possibly all numbers >= some threshold (maybe 20 or 24) can be represented as sum of six lucky numbers? Let's test more.
We need systematic approach.
Observation: Lucky numbers are those containing digits only 0,4,7. They are like numbers in base 10 with digits restricted.
Since we have six numbers, we can consider representing ni in terms of digits with carries. Might be similar to representing any number as sum of 6 numbers each with digits 0,4,7. This is akin to: for each decimal position, we need to select six digits d_j in {0,4,7} such that sum_j d_j + carry_in = digit_ni + 10*carry_out. With six numbers, the total sum of digits per column can be from 0 to 42. The carry_in from lower column is an integer. This is a digit DP that can always find a solution if constraints are not too tight. Since we have many degrees of freedom, maybe any number can be represented, except those with some smallness causing insufficient carries.
Alternatively, note we can use numbers with digits 4 and 7 only, but 0 digits allow padding. Could we use a constructive algorithm: For any large ni, we can first subtract a big lucky number to reduce it, then represent remainder with five numbers (or less). But need guarantee existence.
Maybe we can show that every integer n >= 0 can be represented as sum of at most 6 lucky numbers except some small ones. Could be known result from Codeforces problem (maybe problem "Lucky Sum of Digits"? Actually I recall a Codeforces problem: "Lucky Sum" or "Lucky Numbers" where we represent number as sum of lucky numbers, typically 4 and 7, but here digits are 0,4,7 and numbers are lucky if digits only 4,7? But they included 0 as lucky. Actually problem may be CF Round #153 (Div2) problem? I'm not sure.
Let's search memory: There is a problem "Lucky Numbers (easy2)" maybe? Actually I recall a problem "Lucky Sum" where you need to represent a number as sum of six lucky numbers. I think there is known solution: For any n >= some threshold (like 12) it's always possible; exceptions are numbers 1,2,3,5,6,9,10,13,17 maybe. But we need to confirm.
Let's compute possibilities for sums of up to 6 lucky numbers (allowing zeros). Since lucky numbers are infinite, the set S_k = {sum of k lucky numbers} is a subset of nonnegative integers. With k=6, S_6 may cover all sufficiently large integers. Could be that S_6 is all integers except some small ones.
We can try to compute the Frobenius-like property for this set? But not exactly because numbers are not fixed; we can vary combinations. However, because we have numbers like 4,7,44,47,... they have many values.
Perhaps simpler: Represent any large n using 6 numbers each being either 0, 4, 7, 40, 44, 47, 70, 74, 77, 400,... etc. We could try to use a greedy approach: Take the largest lucky number less than or equal to n, subtract it, and recursively represent remainder with 5 numbers. But that greedy may fail for some n, but maybe with 6 numbers we can always adjust.
Better: Use the concept of representing n in base 10 with digits 0-9, and we want to find six numbers with digits only 0,4,7 such that digit-wise sum matches. This is like a linear combination problem. Since we have six numbers, each digit position sum can be any number that is representable as sum of six digits each from {0,4,7}. That set of possible sums is {0,4,7,8,11,12,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42}? Actually we need to compute all sums of six elements from {0,4,7}. That set is all multiples of 1? Since 4 and 7 are coprime? gcd(4,7)=1, so the set of sums of many numbers (with repetition) yields all sufficiently large integers by the Chicken McNugget theorem for two numbers: The largest integer not representable as a non-negative combination of 4 and 7 is 47-4-7=17. So any integer >= 18 can be expressed as a non-negative combination of 4 and 7 (with any number of terms). But we have exactly six terms, not unlimited. However, we are summing six digits each being either 0,4,7. So the sum is of the form 4a + 7*b where a+b <= 6 (the rest are zeros). So the set of possible sums with exactly six digits (some can be zero) is {4a+7b | a,b >=0, a+b <=6}. Let's list possible values:
a from 0 to 6, b from 0 to 6-a.
Compute:
a=0,b=0:0
a=0,b=1:7
a=0,b=2:14
a=0,b=3:21
a=0,b=4:28
a=0,b=5:35
a=0,b=6:42
a=1,b=0:4
a=1,b=1:11
a=1,b=2:18
a=1,b=3:25
a=1,b=4:32
a=1,b=5:39 (since 14+57=4+35=39, but a+b=6 ok)
a=1,b=4? a+b=5, ok; a=1,b=4=4+28=32; a=1,b=5=39; a=1,b=6? a+b=7 >6 not allowed.
a=2,b=0:8
a=2,b=1:15
a=2,b=2:22
a=2,b=3:29
a=2,b=4:36 (42+74=8+28=36)
a=2,b=5:43? 8+35=43 but a+b=7 >6 not allowed.
a=2,b=3? a+b=5 ok 29; a=2,b=4 => 36, a+b=6 ok.
a=3,b=0:12
a=3,b=1:19
a=3,b=2:26
a=3,b=3:33 (12+21=33)
a=3,b=4:40? 12+28=40, a+b=7 >6 not allowed.
a=3,b=3 =>33, a+b=6 ok.
a=4,b=0:16
a=4,b=1:23
a=4,b=2:30
a=4,b=3:37? 16+21=37, a+b=7 >6 not allowed.
a=4,b=2 =>30 a+b=6 ok.
a=5,b=0:20
a=5,b=1:27
a=5,b=2:34? 20+14=34, a+b=7 >6 not allowed.
a=5,b=1 =>27 a+b=6 ok.
a=6,b=0:24
a=6,b=1:31? 24+7=31, a+b=7 >6 not allowed.
a=6,b=0 =>24.
So possible sums (with exactly six digits, but some can be zeros) are: 0,4,7,8,11,12,14,15,16,18,19,20,21?,21 is from? 0,b=3 gives 21; yes from a=0,b=3:21; 22,23,24,25,26,27,28,29,30,31? Is 31 possible? a=6,b=1 not allowed; a=5,b=1 gives 27; a=4,b=2 gives 30; a=3,b=3 gives 33; a=2,b=4 gives 36; a=1,b=5 gives 39; a=0,b=6 gives 42. Check 31: need 4a+7b=31 with a+b<=6. b=1 =>4a+7=31 =>4a=24 =>a=6, then a+b=7 >6, not allowed. b=2 =>4a+14=31 =>4a=17 not integer. b=3 =>4a+21=31 =>4a=10 no. b=4=>4a+28=31=>4a=3 no. b=5=>4a+35=31 impossible. So 31 not possible. 32: from a=1,b=4? a=1,b=4 gives 4+28=32, a+b=5 <=6 allowed, yes. So 32 is possible. 33: from a=3,b=3 gives 33, a+b=6 allowed. 34: possible? a=5,b=2 gives 20+14=34 but a+b=7 >6; a=2,b=? b=2 gives 22; b=3 gives 29; b=4 gives 36; so 34 not possible. 35: from a=0,b=5 gives 35, a+b=5 allowed. 36: from a=2,b=4 gives 36, a+b=6 allowed. 37? a=4,b=3 => 16+21=37 a+b=7 >6; a=1,b=? 1+? 1+5=39; so 37 not possible. 38? a=3,b=? 3+? 3+2=26; 3+3=33; 3+4=40; a=5,b=2? 5+2=34; a=6,b=1?31; a=0,b=?0+5=35;0+6=42; So 38 not possible. 39: a=1,b=5 gives 39, a+b=6 allowed. 40: a=3,b=4 gives 40 but a+b=7 >6; a=6,b?6+1=31; a=4,b=2=30; a=5,b=1=27; a=0,b=6=42; so 40 not possible? Wait a=3,b=4 not allowed; but a=?? 4a+7b=40, try b=0 =>a=10 not <=6; b=1 =>4a=33 not integer; b=2=>4a=26 no; b=3=>4a=19 no; b=4=>4a=12 =>a=3, a+b=7 >6; b=5=>4a=5 no; b=6=>4a=-2 no. So 40 not possible. 41? b=1=>4a=34 no; b=2=>4a=27 no; b=3=>4a=20 =>a=5, a+b=8 >6; b=4=>4a=13 no; b=5=>4a=6 =>a=1.5 no; b=6=>4a=-1 no. So 41 not possible. 42: a=0,b=6 gives 42, a+b=6 allowed.
Thus possible sums per column (without carry) from the six digits are: {0,4,7,8,11,12,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,32,33,35,36,39,42}. Plus maybe some other values? 13? Not. 31? Not. 34? Not. 37? Not. 38? Not. 40? Not. 41? Not. So there are gaps. However, we also have carries from lower digits, which can increase the effective sum for a column. For a given column, the total input is sum_digits + carry_in, which must equal digit_n + 10*carry_out. Since carry_in can be from 0 up to something, we can fill gaps by having appropriate carry_in values. For example, if we need a total sum of 31, we could have sum_digits = 21 and carry_in = 10? But carry_in is from previous column's carry_out, which is at most floor((previous_sum + previous_carry_in)/10). Since we have 6 digits per column, previous sum can be at most 42, so previous carry_out can be up to floor((42 + previous_carry_in)/10). In principle, carries could be larger because if we have a carry_in from lower column, the sum plus that may cause larger carry_out. However, the maximum carry_in possible is bounded because each column sum can be at most 42 + carry_in. But carry_in can be up to, say, something like 4 or 5? Let's analyze: maximum sum digits = 42. Suppose we have a huge carry_in from previous column? But carry_in is determined by the lower column's sum and its carry_in. Since lower column also limited to 42 digits sum, the maximum carry_out from that column is floor((42 + carry_in_lower)/10). If we optimize to maximize carry, we can get possibly large carries by chain reaction? But carries are bounded by the total sum of the numbers divided by powers of 10; ultimately the overall sum is n, which is given. But we can choose numbers arbitrarily to produce carries. In principle, for a given column, the carry_in can be any non-negative integer, but is it possible to have arbitrarily large carry_in? Suppose we have lower columns that produce a large carry. The sum of lower columns is the sum of the lower digits of the six numbers. Those lower digits can be chosen arbitrarily among {0,4,7}. To get a large carry, we need the sum of digits in lower column plus previous carry to be large. Since the sum of digits in a column is bounded by 42, the maximum carry_out from that column is at most floor((42 + carry_in)/10). So if we want a large carry_in to a higher column, we need to have a chain of columns each producing a carry. Starting from the least significant column, carry_in =0. So the maximum carry into a column is bounded by something like: if we have k columns, the maximum carry can increase, but given we have limited number of columns (since numbers can be arbitrarily large, we can have many columns, but the total sum n is fixed, and we can't exceed n's digits unless we add leading zeros? Actually numbers can have arbitrarily many digits if we allow leading zeros? Leading zeros are not typical, but numbers are represented without leading zeros, but we can consider numbers with extra high digits if we set them to zero? Actually the numbers we output are lucky numbers, they can have arbitrarily many digits? No, lucky numbers are non-negative integers; they have a finite decimal representation without leading zeros except for zero itself. However, we can have numbers with many digits if we want, but we need the sum to equal n. If we choose numbers with digits beyond the highest digit of n, that would increase the sum beyond n unless those digits are zero. But we can have extra digits only if they are zero, so effectively numbers cannot have digits beyond the length of n plus maybe a few for carries? Actually, consider we want sum to be exactly n, so the sum of the numbers must equal n. If we include a number with a non-zero digit at a position beyond the highest digit of n, then the total sum will have that digit, causing n to have that digit unless canceled by others. It's possible to have cancellations via carries? No, digits are non-negative, sum is just addition, no subtraction. So all digits in any column contribute positively. Therefore the highest non-zero digit in the sum must be at most the maximum of the highest digits among the numbers. If we have a number with a digit at a very high place, the sum will have that digit unless another number has a negative digit (not allowed). So we cannot have numbers with digits beyond the length of n (the number of digits of n) because that would make the sum larger than n. However, it is possible to have digits beyond the length of n if they are zero, which is effectively not beyond. So the numbers we use must have at most as many digits as n (or maybe one more due to carry, but the sum of six numbers can produce a carry that increases the length by one; e.g., if n has D digits, the sum of six numbers could have D+1 digits. But then n is given, and if n has D digits, the sum must equal n, so the highest digit must match; if there is an extra carry that produces a digit D+1, then n would need to have that digit as well, but it doesn't, unless the carry from the most significant column results in zero? Actually if we have a carry out of the most significant digit, that creates an extra digit. So for the sum to equal n (which doesn't have that extra digit), the final carry must be zero. So we cannot have a carry out of the most significant digit. So the maximum digit length of the numbers is at most the number of digits of n (maybe we can have an extra digit if the corresponding digit in n is zero? But if we have a number with a digit at position D (0-indexed), then the sum's digit at D would be at least that digit, so n must have that digit at least that; if n's digit at D is 0, we could still have digits that sum to 0 with carry? But sum of non-negative digits cannot be zero unless all digits are zero. So if we have a non-zero digit at a position beyond n's length, that would make n larger. So we cannot have non-zero digits beyond the highest digit of n. So the numbers are limited to at most the number of digits of n.
Thus the number of columns is at most the number of digits of n. The carry into the most significant column must be 0, and the sum digits there must equal n's most digit.
Now, we need to find assignments of digits for each column such that the total sum works. This is like a digit DP where each column we choose six digits from {0,4,7}, and we propagate carry. This is doable because the number of columns is up to 18 (since n up to 1e18, that's at most 19 digits? 1e18 is 19 digits? Actually 10^18 is 1 followed by 18 zeros, so maximum digits is 19? 10^18 is 1e18, which is 19 digits? Let's check: 10^0 =1 (1 digit), 10^1=10 (2 digits), 10^2=100 (3 digits). So 10^18 has 19 digits? 10^18 = 1,000,000,000,000,000,000 -> that's 19 digits? Count: 1 and 18 zeros => 19 digits. Yes. So at most 19 digits. So we could do DP over digits (max 19) with state being the carry (which can be bounded). The maximum carry? Each column sum digits max 42, plus carry_in. The maximum carry_in could be up to something like floor((42+carry_in)/10) but that's recursive. But since number of columns small, we can bound carry by maybe 5 or 6? Let's compute worst-case: If we have a column with sum digits = 42, plus carry_in c, total = 42+c. The carry_out = floor((42+c)/10). To maximize carry_out, we maximize c. c comes from previous column. Starting from c=0, the max carry_out from column is floor(42/10)=4. So after first column, carry <=4. Next column, sum digits max 42, plus carry up to 4 => total up to 46, carry_out up to 4. Actually floor(46/10)=4. So carry stays at most 4. If we try to produce larger carry, we could have sum digits = 42 and carry_in = 9, then total=51, carry_out=5. But can we have carry_in=9? To have carry_in=9 from previous column, that would require total in previous column >= 90. But previous column's sum digits max 42, so total max 42+carry_in_prev. To get carry_out=9, need total >= 90, which would require carry_in_prev >= 48, impossible because max carry_in_prev is limited. Let's formalize: Let c_i be carry into column i (least significant i=0). Initially c_0 = 0. For each column, we choose digits giving sum s_i (0 <= s_i <= 42). Then the digit d_i of n must satisfy: (s_i + c_i) % 10 = d_i, and c_{i+1} = floor((s_i + c_i) / 10). So c_{i+1} is integer >=0. We need to find a sequence s_i in the set S (possible sums from six digits) that satisfies these constraints, and also at the most significant column, after processing all digits, we need final carry c_{L}=0 (where L is number of digits). Since n may have leading zeros? Actually n is given without leading zeros, so the most significant digit is non-zero. We need c_{L}=0.
Thus we have a DP with state c (carry) at each digit, and we need to choose s in S. c can be at most something. Since s_i <= 42, and c_i <= something, we can compute maximum possible c_i. Since each step c_{i+1} = floor((s_i + c_i)/10) <= floor((42 + c_i)/10). Starting with c_0=0, max c_1 = floor(42/10)=4. Then max c_2 = floor((42+4)/10)= floor(46/10)=4. So it stays at 4. If we get s_i=42 and c_i=4 => total=46 => c_{i+1}=4. So maximum carry seems 4. But could we have a scenario where c_i >4? Suppose we set s_i=42 and c_i=5 (if possible) => total=47 => c_{i+1}=4. So still 4. Actually from formula, if c_i <=5, floor((42+5)/10)=4. So c cannot exceed 4 after first step. But could c_i be 5 initially? Starting from 0, the first c1 max 4, so c_i never reaches 5. However, what about if s_i is less than 42 but we could have carry from previous column being 4, then total max 46, carry_out 4. So maximum carry is 4. But wait, what about if we have many columns, but the bound holds. So the carry is between 0 and 4 inclusive. Possibly up to 5 if we consider that s_i can be >42? No, s_i max is 42 as we computed (six digits each max 7). So indeed carry <= 4.
But is there possibility of c=5? Let's check if s_i + c_i >= 50 for some c_i? For c_i=5, s_i max 42 => total 47 <50, so floor(47/10)=4. So c_{i+1} would be at most 4. So c cannot be 5. So carry state space is small: 0 to 4.
Thus DP over at most 19 digits with carry 0..4 is trivial. For each digit of n (from least significant to most), we need to find s in S such that (s + c) % 10 = digit, and then new carry = floor((s+c)/10). If we can find a path from least significant digit to most significant with final carry 0, we have a solution.
We also need to reconstruct the six numbers. After we determine s_i (the sum of the six digits at column i) for each digit position, we then need to decompose each s_i into six digits each in {0,4,7} (i.e., find a 6-tuple of digits that sum to s_i). Then those digits become the digits of the six numbers (the i-th digit of each number). So we also need for each column i to output a specific assignment of six digits (0,4,7) that sum to s_i. Then the six numbers are formed by reading the digits across columns (least significant to most). That yields six lucky numbers (since each digit is 0,4,7). They may have leading zeros, but we output them as numbers, so leading zeros are omitted (i.e., the number is 0 if all digits are zero). That's fine.
Thus the problem reduces to: For each n, run DP over its decimal digits (up to 19 digits) to determine if there exists a sequence of s_i from set S (possible sums of six digits each 0,4,7) such that the digit constraints hold. If yes, then also produce for each column a decomposition of s_i into six digits from {0,4,7}. Then output the six numbers.
Given t up to 5000, n up to 1e18, this DP is very fast.
We must also consider the possibility that n has many digits; DP per test is O(num_digits * |S| * carry_states) which is tiny.
Now, we need to confirm that the set S we computed (possible sums of six digits from {0,4,7}) is correct. But note: digits are per column. For each column, we are assigning to the six numbers a digit (0,4,7). The sum of these six digits is s_i. We computed S as all numbers that can be expressed as sum of six numbers each from {0,4,7}. That's correct.
But we must ensure that we can always find a decomposition of any s in S into six digits from {0,4,7}. That is a small combinatorial problem; we can precompute all possible 6-tuples for each s in S. There may be multiple; we can choose any.
Now, the DP: Let digits of n be array d[0..L-1] where d[0] is least significant. We'll process i=0..L-1. State: carry c (0..4). For each possible s in S, if (s + c) % 10 == d[i], then new_carry = (s + c) / 10 (integer division). Proceed. At the end after processing all digits, we need final carry = 0.
But also note that after processing all digits, we might have a final carry >0, which would mean the sum has an extra digit; that would require n to have that digit, but it doesn't. So final carry must be 0.
Thus DP is straightforward. We also need to handle the case where L is the number of digits; after processing all given digits, if there is still a carry, we could potentially consider additional digits (leading zeros of n) to absorb the carry. That is, we can consider that n has extra leading zeros (i.e., we can treat n as having as many digits as needed to allow the carry to be resolved). Since numbers can have more digits than n, but the sum must equal n exactly. If we have a final carry after processing the most significant digit of n, that would mean the sum of numbers produces a higher digit, making the total > n. However, we could have the most significant digit of n be such that the carry is 0. But if after processing all digits of n, we have carry > 0, then we cannot match n because that would require an extra digit in n. Conversely, if we have a carry after processing the most significant digit, we could consider that n has an extra digit (i.e., we can treat n as having a leading digit that is actually the carry). But n is fixed, we cannot change it. So the DP must ensure that after processing the most significant digit, the carry becomes 0. However, we could allow extra columns beyond the given digits if those columns would contribute zero to the sum? Wait: The sum of the six numbers equals n. If we include extra columns (higher digits) that are not present in n, those digits would be zero in n. But we can set the digits in those columns for the six numbers to be zero, then the sum's higher digits would be zero. So we cannot have non-zero digits beyond the length of n because that would make the sum larger. So we must have all digits beyond the most significant digit of n to be zero. That means after processing the most significant digit of n, the carry must be zero. However, it's possible that we need to have an extra column to absorb a carry? Actually, the process: We process columns from least significant upwards. Suppose we have L digits of n. After processing the (L-1)-th digit (most significant), we compute new carry c_{L}. For the sum to equal n, we need c_{L}=0, because there is no digit beyond that. If c_{L} > 0, then the sum would have an extra digit c_{L} at position L, making the total > n. So we must have c_{L}=0.
Thus DP must ensure that.
Now, is it always possible for large n? Let's test some small n via DP to see which are impossible. We can compute S and run DP for n from 1 upwards to see which are impossible. But since we have to implement for up to 1e18, we can just run DP for each test.
But we must be careful: The DP is per test, and we need to reconstruct the six numbers. That's easy.
We'll need to precompute the set S of possible sums of six digits from {0,4,7} (with exactly six digits? Actually we have exactly six numbers, so we must use exactly six digits per column. Some of those digits may be 0. So yes, we sum exactly six digits each from {0,4,7}. So S as computed.
Now, we need also for each s in S, a way to get a 6-tuple of digits (d1..d6) each in {0,4,7} summing to s. We can precompute a mapping from s to a vector of 6 digits (maybe by enumerating all combinations? There are 3^6 = 729 combinations; we can iterate all combos and for each sum store a representative. That's easy.
Now, the DP: Since digits up to 19 and carry small, we can do a simple recursion/backtracking with memoization, but better to do iterative DP storing a "parent" choice that leads to success. For each position i and carry c, we need to know if it's possible to process from that position to the end. We'll process from least significant to most? Typically DP from most significant? But easier: We'll do DP from least significant to most significant with state (pos, carry). But we need to know which s we chose. We can do forward DP: start at pos=0 with carry=0. For each state (pos, carry), try s in S; if (s+carry)%10 == digit[pos], then new_carry = (s+carry)/10; set next state (pos+1, new_carry) as reachable. After processing all digits (pos == L), we need carry == 0. So we can do BFS/DP forward. Since L is small, we can just do recursion with memo.
But we also need to reconstruct the s_i for each position. So we can store for each state (pos, carry) the chosen s that leads to a solution (or more precisely, for each state we can record which s we used to go to a valid next state). We'll compute DP backwards: define reachable(pos, carry) = whether we can process digits from pos (starting at pos index) to end given current carry into this pos. We'll start from the most significant? Actually easier: do recursion from least significant to most with memo and backtrack.
But careful: The DP from least significant: At position i (0-index LSB), we have carry_in c. We need to choose s such that (s + c) % 10 = d_i, and then new_carry = floor((s+c)/10). Then we need to process i+1 with new_carry. Base case: when i == L (beyond last digit), we need c == 0.
So we can define a function f(i, c) returns bool (possible). We'll compute bottom-up or top-down. Since L <= 19 and c in 0..4, total states <= 100, we can do recursion with memo.
Then for reconstruction, we store for each (i,c) the s that worked (and the resulting new_carry). Then we can reconstruct s_i for each i.
After we have s_i for each column i (0..L-1), we need to assign six digits for each column. We have precomputed a 6-tuple for each s (sum) from the set S. But we must ensure that the six digits we assign per column collectively form six numbers when read across columns. That is, for each of the six numbers, its digits across columns are the digits we assign for that number. Since we have precomputed for each column a tuple of six digits that sum to s_i, we can simply, for number j (1..6), collect its digit from each column (starting from LSB) to form its value. But we need to ensure that the numbers are lucky: they should consist only of digits 0,4,7. Since we assign each digit from {0,4,7}, that's satisfied. Also, leading zeros are allowed; we output the integer without leading zeros (i.e., if all digits for a number are zero, output 0). That's fine.
However, there is a nuance: When we assign digits per column independently, we might produce numbers that have leading zeros. That's fine.
But we must also ensure that the digits we assign across columns for a given number are consistent with the number's decimal representation: That is, we must assign digits to each number for each column. Since we process columns independently, we can simply for each column i, pick a tuple (d1..d6) from our precomputed mapping for s_i. But we need to be careful: The mapping should be consistent across columns? No, each column's assignment of digits to the six numbers is independent; we can choose any decomposition for each column. There's no requirement that the same number's digits across columns need to follow any rule; they just form the number. So we can assign for each column a tuple arbitrarily as long as the tuple sums to s_i. Then the j-th number will have digit from column i equal to the j-th element of the tuple for column i. That's valid because the digits per column are independent; the sum of the numbers equals n by construction because column sums match the s_i and carry propagation. So it's fine.
Thus the problem reduces to a DP on digits with a small set S.
Now, we should verify that for all n (up to 1e18) the DP yields a solution, except some small n. But we must confirm that for large n, it's always possible. We can test with DP for many numbers to see if any fails. But given the constraints, we can just run DP for each test case. It's fast.
But we need to ensure that the DP will always find a solution for large n. However, is it guaranteed that for all sufficiently large n, the DP will succeed? Let's think: The set S is limited to certain values. With carries up to 4, the total (s + c) modulo 10 can produce any digit? We need to see if for each digit d and given carry c, there exists s in S such that (s + c) % 10 = d and the new carry is something that allows continuation. So it's a kind of automaton. We need to test if from state c=0 at the most significant digit (when processing from LSB to MSB? Actually final condition is carry=0 after MSB). The DP will tell us.
I can try to reason about the existence. Since S includes many numbers, maybe the DP always works for n >= some threshold. But I'd rather trust the DP.
One more nuance: We must output exactly six numbers. Our construction uses exactly six numbers (since we always assign six digits per column). So that's fine.
Now, we need to ensure that the DP works even when n has leading zeros? No, n is positive integer, so most significant digit non-zero.
Implementation Plan:
Precomputation:
-
Enumerate all 6-digit tuples (a1,...,a6) where each ai in {0,4,7}. There are 3^6 = 729 combos. For each tuple, compute sum = s. Store for each s (0..42) one representative tuple (or maybe multiple, but one is enough). We need a mapping from s to a 6-element vector (digits). Since some s may not be achievable; we only care about s in S. We'll store for each s in S a vector of size 6.
-
Also store the set S as a vector of possible sums.
DP per test:
-
Convert n to decimal digits in an array (least significant first). Let L = number of digits.
-
Create DP table: dp[i][c] where i from 0..L (i is position index, 0 = LSB), c from 0..4 (maybe 5? but we argued max 4). dp[i][c] = bool whether it's possible to process digits from i to L-1 with carry_in c, and end with carry 0 after last digit. Actually we need to process all digits and final carry 0. So we can define dp[i][c] for i from 0 to L (i is next digit index). Base: dp[L][c] = (c == 0). For i from L-1 down to 0, we try s in S, compute if (s + c) % 10 == digit[i], then new_carry = (s + c) / 10, and if dp[i+1][new_carry] is true, then dp[i][c] = true and store choice[i][c] = s (and maybe new_carry). We'll compute backwards (from most significant to least?) Actually easier: iterate i from L down to 0. Let's define dp[pos][carry] meaning from position pos (0-indexed, starting at pos) to the end (pos..L-1) with current carry into this position, we can succeed. Then recurrence: for each s in S, if (s+carry)%10 == digit[pos], then new_carry = (s+carry)/10, and if dp[pos+1][new_carry] true, then dp[pos][carry] true and record s. So we need to compute from pos = L-1 down to 0. We'll initialize dp[L][c] = (c==0). Then for pos = L-1 to 0, for each c in 0..4, compute.
We must also ensure that new_carry stays within 0..4. But from our bound, new_carry will be at most floor((max s + max c)/10) = floor((42+4)/10)=4, so it's within 0..4. However, if we have c up to 4, s can be up to 42, so s+c up to 46, floor division yields at most 4. So we can restrict new_carry to 0..4. But if s=0 and c=0, new_carry=0. So yes, range 0-4.
But note: s can be 0..42, c 0..4, so s+c up to 46. The division yields values from 0 to 4. Good.
Now, we also need to consider the possibility that n has up to 19 digits, but the DP might need to consider extra leading zeros beyond the most significant digit to allow a final carry to be absorbed? But final carry must be 0 after processing the given digits. However, if after processing all digits we have carry >0, we could add an extra digit to n (a leading zero) to absorb? But that would change the number; we cannot. So we must have carry=0 at the end.
Thus DP as described works.
Now, we need to consider if there is any n where dp[0][0] is false. That would mean impossible. For such n, output -1.
Now, we should test with examples: 42, 17, 444, 7, 51.
Let's compute manually or via quick mental DP:
First, list S: We'll list all s that are possible: 0,4,7,8,11,12,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,32,33,35,36,39,42. Also maybe 31? Not. 34? Not. 37? Not. 38? Not. 40? Not. 41? Not. 42? Yes. Also check if 1,2,3,5,6,9,10,13,17, etc are not in S. Good.
Now, example: n=42. Digits: [2,4]? Actually 42: digits: LSB=2, next=4. So d[0]=2, d[1]=4.
We start from pos=0, carry=0. Need s such that (s+0)%10 =2 => s%10=2. Which s in S have last digit 2? S: 0,4,7,8,11,12,14,15,16,18,19,20,21,22,23,24,25,26,27,28,29,30,32,33,35,36,39,42. Those with last digit 2: 12? last digit 2; 22; 32; 42? last digit 2? 42 ends with 2, yes; also 2? No. So s=12,22,32,42 are candidates. For each, compute new_carry = floor(s/10). For s=12, new_c=1; s=22, new_c=2; s=32, new_c=3; s=42, new_c=4.
Now we need to process pos=1 (digit=4) with carry = new_c, and then after pos=1 we need final carry=0.
Let's check each:
-
s=12, new_c=1: At pos=1, carry=1, need (s2+1)%10=4 => s2%10=3. s in S with last digit 3: Which? 23? ends with 3; 33? ends with 3; also 3? not in S; 13? not; 43? not; 53? not. So possible s2=23,33. Compute new_c2 = floor((s2+1)/10). For s2=23: total=24 -> new_c2=2; then after pos=1, we need dp[2][2] (pos beyond digits) base condition: dp[2][2] = (2==0)? false. So fails. For s2=33: total=34 -> new_c2=3; dp[2][3] false. So s=12 fails.
-
s=22, new_c=2: At pos=1, carry=2, need (s2+2)%10=4 => s2%10=2. s2 with last digit 2: 12,22,32,42. For each, compute total and new_c2: s2=12 -> total=14 -> new_c2=1; dp[2][1] false. s2=22 -> total=24 -> new_c2=2 false; s2=32 -> total=34 -> new_c2=3 false; s2=42 -> total=44 -> new_c2=4 false. So fails.
-
s=32, new_c=3: need s2%10 = (4 - 3) mod10 = 1? Actually (s2+3)%10=4 => s2%10=1. s in S with last digit 1: 11,21,31? 31 not; 41? not; 1? not. So s2=11,21. Check: s2=11, total=14 -> new_c2=1 false; s2=21, total=24 -> new_c2=2 false. So fails.
-
s=42, new_c=4: need s2%10 = (4-4)=0 => s2%10=0. s in S with last digit 0: 0,20,30,40? 40 not; so s2=0,20,30. Check: s2=0, total=4 -> new_c2=0 -> dp[2][0] true (base). So works! So we have s0=42, s1=0.
Thus representation: For least significant column (10^0), sum of digits = 42; for next column (10^1), sum = 0. Need to decompose: s0=42 into six digits from {0,4,7} that sum to 42. Since 42 = 6*7, we can take all six digits as 7. That gives each number digit 7 in units place. s1=0, all digits 0. So numbers: each number gets digit 7 in units place, and 0 in tens place. So the six numbers are all 7. So output "7 7 7 7 7 7". That matches example.
Next example: n=17. Digits: [7,1] (LSB=7, MSB=1). Let's run DP.
S as before.
Pos0, c=0: need s%10 =7. Which s end with 7? 7, 27? 27 ends with 7; 37? not; 47? not; 17? not in S (17 not). So s=7 and 27? 27 is in S (we have 27). Also maybe 7 only? Also 7,27. Also 47? not. 57? no. 67? no. So s=7 and 27. Also 107? no. Check S: 27 is there. Also 7 is there. Also 37? not. 47? not. So candidates.
-
s=7, new_c=0 (since 7/10=0). Then pos1, c=0, need (s2+0)%10 = 1 => s2%10=1. s2 with last digit 1: 11,21. Also 31? not; 1? not. So s2=11 or 21. Check new_c2: For s2=11, total=11 -> new_c2=1; dp[2][1] false. For s2=21, total=21 -> new_c2=2; false. So fails.
-
s=27, new_c=2 (since 27/10=2). pos1, c=2, need (s2+2)%10=1 => s2%10=9? (1-2 mod10 =9). s2%10=9. Which s in S end with 9? 39? ends with 9; 29? ends with 9? 29 ends with 9; 19 ends with 9; 9? not in S. So s2=19,29,39. Check:
s2=19: total=19+2=21 -> new_c2=2; dp[2][2] false.
s2=29: total=29+2=31 -> new_c2=3; false.
s2=39: total=39+2=41 -> new_c2=4; false.
So fails.
Thus no solution, output -1. Matches.
Next example: n=444. Digits: LSB=4, next=4, next=