Task
Your task is to find the lexicographically lowest -gcd equivalent permutation of a given sequence of positive integers . Two sequences (that are permutations of each other) and are considered -gcd equivalent if for every set of distinct indices from to , the greatest common divisor (gcd) of the elements in these positions in both sequences is the same.
However, there's a twist - you are allowed to multiply at most one element of by a given integer before finding this lowest -gcd equivalent sequence. You are allowed to not multiply by at all and keep the same sequence. Additionally, it is guaranteed that is divisible by only and itself. You aim to minimize the resulting sequence lexicographically among all possible choices of preprocessing (or choosing not to) of .
Write a program that solves this problem.
Input data
The first line of the input contains one integer , the number of test cases. Each test case consists of three positive integers , , and , followed by positive integers .
Output data
For each test case, output integers representing the lexicographically lowest -gcd equivalent sequence to after performing the allowed preprocessing.
Constraints and clarifications
- (over all test cases)
- , is either or prime
# | Points | Required subtasks | N | X | Additional constraint |
---|---|---|---|---|---|
1 | 6 | - | - | ||
2 | 19 | 1 | - | ||
3 | 13 | 1 - 2 | - | ||
4 | 4 | 1 | - | - | |
5 | 4 | 1, 2, 4 | - | - | |
6 | 31 | - | - | for | |
7 | 23 | 1 - 6 | - | - |
- Points for a given subtask are awarded only if all tests specified for it are successfully passed.
Example
stdin
2
3 2 1
2 6 4
4 2 3
7 3 6 9
stdout
2 4 6
3 6 9 21
Explanation
Two test cases. For the first one, no preprocessing is required.
The -gcd property is satisfied since the greatest common divisor of all pairs remains . For the second one, preprocessing by multiplying the first element by results in the lexicographically lowest sequence .