Under construction

Still being built. Things may change

LEX_GCD

Time limit1.5sMemory limit1024 MB

Summary
Find the lexicographically smallest permutation that is K-gcd equivalent to a sequence, after optionally multiplying one element by a prime X.
Level

Hard8 of 10

Topics
Number theory, Greedy, Sorting
Solved
No attempts yet

Problem

Your task is to find the lexicographically lowest KK-gcd equivalent permutation of a given sequence of NN positive integers a_1,a_2,…,a_Na\_1 , a\_2 , \dots , a\_N. Two sequences that are permutations of each other, aa and bb, are KK-gcd equivalent if, for every set of KK distinct indices from 11 to NN, the gcd of the elements at those positions is the same in both sequences.

You may multiply at most one element of aa by a given integer XX before searching. You may also leave aa unchanged. XX is either 11 or a prime, so its only divisors are 11 and XX. If you multiply an element, the result must be a KK-gcd equivalent of the modified aa, with the gcds computed after the multiplication.

Input

The first line 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 a_1,a_2,…,a_Na\_1 , a\_2 , \dots , a\_N.

Output

For each test case, print NN integers: the lexicographically lowest KK-gcd equivalent sequence of aa after the optional preprocessing.

Constraints

  • 2≤∑N≤1052 \le \sum N \le 10^5 (over all test cases)
  • 2≤K≤N2 \le K \le N
  • 1≤X≤1091 \le X \le 10^9, and XX is either 11 or prime
  • 1≤a_i≤1091 \le a\_i \le 10^9

Examples1

  1. Example 1

    Input
    2
    3 2 1
    2 6 4
    4 2 3
    7 3 6 9
    
    Expected output
    2 4 6
    3 6 9 21