#P1918B. Minimize Inversions
Minimize Inversions
Description
You are given two permutations and of length . A permutation is an array of elements from to where all elements are distinct. For example, an array [] is a permutation, but [] and [] aren't.
You can (as many times as you want) choose two indices and , then swap with and with simultaneously.
You hate inversions, so you want to minimize the total number of inversions in both permutations.
An inversion in a permutation is a pair of indices such that and . For example, if then there are inversions in it (the pairs of indices are , and ).
The first line contains an integer () — the number of test cases.
Each test case consists of three lines. The first line contains an integer () — the length of the permutations and . The second line contains integers () — permutation . The third line contains in a similar format.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output two permutations and (in the same format as in the input) — the permutations after all operations. The total number of inversions in and should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.
If there are multiple solutions, print any of them.
Input
The first line contains an integer () — the number of test cases.
Each test case consists of three lines. The first line contains an integer () — the length of the permutations and . The second line contains integers () — permutation . The third line contains in a similar format.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output two permutations and (in the same format as in the input) — the permutations after all operations. The total number of inversions in and should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.
If there are multiple solutions, print any of them.
Note
In the first test case, the minimum possible number of inversions is .
In the second test case, we can sort both permutations at the same time. For this, the following operations can be done:
- Swap the elements in the positions and in both permutations. After the operation, [], [].
- Swap the elements in the positions and . After the operations, and are sorted.
In the third test case, the minimum possible number of inversions is .