import java.io.PrintWriter;
import java.util.*;

public class Main {

    static int[][] Memo = new int[1001][1001];
    static int GCD(int a, int b) {
        if (b < 2) return b;
        if (Memo[a][b] == 0) {
            if (a % b == 0) Memo[a][b] = b;
            else Memo[a][b] = GCD(b, a % b);
        }
        return Memo[a][b];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int test = scanner.nextInt();
        while (test-- > 0) {
            int n = scanner.nextInt();
            int []N = new int[n];
            int bi = 0;
            int bg = 0;
            for (int i = 0; i < n; i++) {
                N[i] = scanner.nextInt();
                for (int j = 0; j < i; j++) {
                    int gcd = GCD(N[i], N[j]);
                    if (gcd > bg) {
                        bg = gcd;
                        bi = j;
                    }
                    if (gcd == bg) {
                        if (N[i] > N[bi]) bi = i;
                        if (N[j] > N[bi]) bi = j;
                    }
                }
            }
            System.out.print(N[bi]);
            int currentGCD = N[bi];
            N[bi] = -1;
            for (int i = 0; i < n - 1; i++) {
                bg = -1;
                bi = -1;
                for (int j = 0; j < n; j++) {
                    if (N[j] == -1) continue;
                    int gcd = GCD(N[j], currentGCD);
                    if (gcd > bg) {
                        bg = gcd;
                        bi = j;
                    }
                }
                System.out.print(" " + N[bi]);
                currentGCD = bg;
                N[bi] = -1;
            }
            System.out.println();

        }
    }
}
