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

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

    public static void main(String[] args)throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int qqq = sc.nextInt();
        while (qqq-->0){
            int n = sc.nextInt();
            int[] arr = new int[n];
            PriorityQueue<Integer>p = new PriorityQueue<>(Collections.reverseOrder());
            for(int i=0;i<n;i++)p.add(sc.nextInt());
            for(int i=0;i<n;i++)arr[i] = p.poll();
            int gcd = arr[0];
            for(int i=1;i<n;i++){
                int ind = i;
                int max = 1;
                for(int j=i;j<n;j++){
                    if(max<gcd(gcd,arr[j])){
                        ind = j;
                        max = gcd(gcd,arr[j]);
                    }
                }
                int t = arr[ind];
                arr[ind] = arr[i];
                arr[i] = t;
                gcd = gcd(arr[i],gcd);
            }
            for(int i=0;i<n;i++){
                pw.print(arr[i]+" ");
            }
            pw.println();
        }
        pw.flush();
        pw.close();
    }
    static class Pair implements Comparable<Pair>{
        int x;
        int y;
        public Pair(int x,int y){
            this.x= x;
            this.y = y;
        }
        public int compareTo(Pair p){
           int max = Math.max(x,y);
           int max2 = Math.max(p.x,p.y);
           if(max==max2){
               int min = Math.min(x,y);
               int min2 = Math.min(p.x,p.y);
               return Long.compare(min,min2);
           }else{
               return Long.compare(max,max2);
           }
        }

        @Override
        public String toString() {
            return x+" " + y;
        }
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();

        }
    }
}
