import java.io.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    private static int gcd(int a, int b) {
        if (b==0) return a;
        return gcd(b,a%b);
    }

    public static void main(String[] args) throws IOException{
        PrintWriter out = new PrintWriter(System.out);
        //QuickInput sc = new QuickInput();
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int tt = 0; tt<t; tt++) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++)
                a.add(sc.nextInt());
            int max = 0;
            for (int i = 1; i < n; i++) {
                if (a.get(i) > a.get(max))
                    max = i;
            }
            int b[] = new int[n];
            b[0] = a.get(max);
            a.remove(max);
            int grc = b[0];
            for(int i = 1; i<n; i++){
                int can = 0;
                for(int j = 0; j<a.size(); j++){
                    if(gcd(grc, a.get(j))>gcd(grc, a.get(can)))
                        can = j;
                }
                b[i] = a.get(can);
                grc = gcd(grc, a.get(can));
                a.remove(can);
            }
            for(int i = 0; i<n; i++){
                System.out.print(b[i]+" ");
            }
            System.out.println();
        }
        out.flush();
    }
}