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

public class B {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(f.readLine());
        while(t-->0){
            int n = Integer.parseInt(f.readLine());
            long[] arr = new long[n];
            StringTokenizer st = new StringTokenizer(f.readLine());
            long max = 0;
            int cmi = -1;
            for(int i = 0; i < n; i++){
                arr[i] = Long.parseLong(st.nextToken());
                if(arr[i] > max) {
                    max = arr[i];
                    cmi = i;
                }
            }
            long[] outarr = new long[n];
            long runGCD = max;
            outarr[0] = max;
            boolean[] seen = new boolean[n];
            seen[cmi] = true;
            for(int i = 1; i < n; i++){
                long maxGCD = 0;
                int maxidx = -1;
                for(int j = 0; j < n; j++){
                    if(seen[j]) continue;
                    long curgcd = gcd(runGCD, arr[j]);
                    if(curgcd > maxGCD){
                        maxGCD = curgcd;
                        maxidx = j;
                    }
                }
                outarr[i] = arr[maxidx];
                runGCD = gcd(runGCD, outarr[i]);
                seen[maxidx] = true;
            }
            StringBuilder ans = new StringBuilder();
            for(int i = 0; i < n; i++) ans.append(outarr[i]+" ");
            out.println(ans.toString().trim());


        }

        out.close();
    }
    public static long gcd(long a, long b){
        while(b != 0){
            long t = b;
            b = a%b;
            a = t;
        }
        return a;
    }
}
