
import java.util.Scanner;

/*
* given n positive integers a1, a2, ..., an
* make a sequence b1, b2... bn such that
* sequence c1, c2, c3... cn is lexicographically maximal
* ci = GCD(b1, ..., bi)
*
* a sequence is lexicographically smaller that a sequence b if:
*   -> a is a prefix of b, but a != b
*   -> int the first position where a and b differ, the sequence a has a smaller element than b
*
* */
public class BigVova {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int testCases = sc.nextInt();

        for (int i = 0; i < testCases; i++) {
            int sequenceLength = sc.nextInt();
            int[] sequence = new int[sequenceLength];
            for (int j = 0; j < sequenceLength; j++)
                sequence[j] = sc.nextInt();
            printSequence(sequence);
        }
    }

    private static void printSequence(int[] sequence) {
        int maxIndex = findMax(sequence);
        System.out.print(sequence[maxIndex]);
        int gcd = sequence[maxIndex];
        sequence[maxIndex] = -1;
        int nextGCD;
        int numsPut = 1;

        while (numsPut != sequence.length) {
            nextGCD = 0;
            for (int i = 0; i < sequence.length; i++) {
                if (sequence[i] != -1) {
                    int trial = findGCD(gcd, sequence[i]);
                    if (trial > nextGCD) {
                        nextGCD = trial;
                        maxIndex = i;
                    }
                }
            }
            System.out.printf(" %d", sequence[maxIndex]);
            sequence[maxIndex] = -1;
            gcd = nextGCD;
            if (gcd == 1) {
                for (int i = 0; i < sequence.length; i++)
                    if (sequence[i] != -1)
                        System.out.printf(" %d", sequence[i]);
                break;
            }
            numsPut++;
        }
        System.out.println();
    }

    public static int findGCD(int a, int b) {
        if (b == 0)
            return a;
        else return findGCD(b, a % b);
    }

    private static int findMax(int[] sequence) {
        int maxIndex = 0;
        for (int i = 0; i < sequence.length; i++)
            if (sequence[i] > sequence[maxIndex])
                maxIndex = i;

        return maxIndex;
    }
}
