There are two allowed edit operations on positive integers: reshuffle and resize.
A reshuffle rearranges the order of digits arbitrarily. For the purpose of a reshuffle we will assume that each number can start with arbitrarily many leading zeros and those can be rearranged as well. For example, you may reshuffle \(104\) to \(40\,010\), \(40\,010\) back to \(104\), \(11\,234\) to \(41\,213\), or \(1\) to a googol (\(10^{100}\)).
A resize modifies the value of number. The width of a resize is the absolute difference between the original and the new number. E.g., a resize of width 3 can change 158 into either 155 or 161.
Reshuffles are completely free.
Given a sequence of edits, the length of that sequence is the number of resizes, the width of the sequence is the maximum width of a resize used (or zero if there are no resizes), and the area of the sequence is the product of its length and width.
Given positive integers \(a\) and \(b\), find the minimum area of an edit sequence that changes \(a\) to \(b\), and construct one such sequence.
The first line of each input file contains the number \(t\) of test cases.
Each of the following \(t\) lines contains two positive integers \(a\) and \(b\).
Subtask M4 has a special output format, the difference is explained below.
For each test case output three lines:
More precisely, your output must satisfy the following constraints:
It is guaranteed that for each test case in subproblems M1-M3 there is always an optimal sequence of edits that can be output in the above format.
(Also, for these tests the bounds “at most 50 digits” and “\(y\leq 500\)” should both be reasonably loose to not affect valid solutions.)
In each of the subproblems M1-M3, 50% of points will be awarded for an output file which is not completely correct but satisfies the following criteria:
Input file: M1.in
Constraints: \(t=100\) and in each test \(1 \leq a,b < 10^5\).
Input file: M2.in
Constraints: \(t=100\) and in each test \(1 \leq a,b < 10^{16}\).
Input file: M3.in
Constraints: \(t=100\) and in each test \(1 \leq a,b < 10^{25}\).
Input file: M4.in
Constraints: \(t=100\) and in each test \(1 \leq a,b < 10^{1000}\).
In this subproblem only output one line per test case with the optimal value \(x\).
(The full output file with constructed solutions would be too large to submit.)
For this subtask there is no partial score.
input2 47 47 4200 7017 | output0 0 47 6 5 4200 420 417 174 7014 7017 |
The sample solution to the second test case contains two resizes: in the second step we decrease the number by 3 and in the last step we increase it by 3. Thus, the width of this solution is \(\max(3,3) = 3\), its length is 2, and therefore its area is \(3\times 2 = 6\).
Note that the number of reshuffles does not matter. In particular, in the sequence shown above the reshuffle \(417\to 174\) can easily be omitted and the solution would still be valid.