LEX_GCD

Time limit: 1.5s Memory limit: 512MB Input: Output:

Task

Your task is to find the lexicographically lowest KK-gcd equivalent permutation of a given sequence of NN positive integers a1,a2,,aNa_1, a_2, \ldots, a_N. Two sequences (that are permutations of each other) a1,a2,,aNa_1, a_2, \ldots, a_N and b1,b2,,bNb_1, b_2, \ldots, b_N are considered KK-gcd equivalent if for every set of KK distinct indices from 11 to NN, 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 aa by a given integer XX before finding this lowest KK-gcd equivalent sequence. You are allowed to not multiply by XX at all and keep the same sequence. Additionally, it is guaranteed that XX is divisible by only 11 and itself. You aim to minimize the resulting sequence lexicographically among all possible choices of preprocessing (or choosing not to) of aa.

Write a program that solves this problem.

Input data

The first line of the input contains one integer TT, the number of test cases. Each test case consists of three positive integers NN, KK, and XX, followed by NN positive integers a1,a2,,aNa_1, a_2, \ldots, a_N.

Output data

For each test case, output NN integers representing the lexicographically lowest KK-gcd equivalent sequence to aa after performing the allowed preprocessing.

Constraints and clarifications

  • 2N1052 \leq \sum_{}{} N \leq 10^5 (over all test cases)
  • 2KN2 \leq K \leq N
  • 1X1091 \leq X \leq 10^9, XX is either 11 or prime
  • 1ai1091 \leq a_i \leq 10^9
# Points Required subtasks N X Additional constraint
1 6 - N50\sum N \leq 50 X=1X = 1 -
2 19 1 N1000\sum N \leq 1000 X=1X = 1 -
3 13 1 - 2 N105\sum N \leq 10^5 X=1X = 1 -
4 4 1 N100\sum N \leq 100 - -
5 4 1, 2, 4 N1000\sum N \leq 1000 - -
6 31 - N105\sum N \leq 10^5 - aiaja_i \neq a_j for iji \neq j
7 23 1 - 6 N105\sum N \leq 10^5 - -
  • 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 KK-gcd property is satisfied since the greatest common divisor of all pairs remains 22. For the second one, preprocessing by multiplying the first element by 33 results in the lexicographically lowest sequence 3,6,9,213, 6, 9, 21.

Log in or sign up to be able to send submissions!