

import java.io.*;

import java.util.*;


public class Main {


    public static void main(String[] args) throws IOException {

        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        while(t--!=0){
            int n = s.nextInt();
            int arr[] = new int[n];
            int freq[] = new int[n+1];
            int maximo = -5,idx=0;
            for(int i=0;i<n;i++){
                arr[i] = s.nextInt();
                if(arr[i]>maximo){
                    maximo = arr[i];
                    idx = i;
                }
            }

            if(n==1){
                System.out.println(arr[0]);
            }

            else{
                int max ;
                int res [] = new int[n];
                //int akbr = -2;
                res[0] = maximo;freq[idx] = 1;
                max = res[0];

                /*for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        if(j!=i){
                            int x =gcd(arr[i],arr[j]);
                            if(x>=max){
                                max = x;
                                if(Math.max(arr[i],arr[j])>akbr){
                                    akbr = Math.max(arr[i],arr[j]);
                                    res[first] = akbr;
                                    res[second] = arr[i]+arr[j]-akbr;
                                    a=i;b=j;
                                }
                            }
                        }
                    }
                }*/

                int acc = 1;

                while(true){
                    if(acc==n)break;
                    else{
                        int maxi = -1,element,mkan=0;
                        for(int i=0;i<n;i++){
                            if(freq[i]==0){
                                int x = gcd(max,arr[i]);
                                if(x>maxi){
                                    element = arr[i];
                                    mkan = i;
                                    maxi = x;
                                }
                            }
                        }
                        acc++;
                        max = maxi;
                        res[acc-1] = arr[mkan];
                        freq[mkan] = 1;
                    }
                }

                for(int i=0;i<n;i++) System.out.print(res[i]+" ");
                System.out.println();
            }


        }

    }


    public static int gcd(int a,int b){

        if(b==0)return a;
        else return gcd(b,a%b);

    }



}




class Scanner
{
    StringTokenizer st;
    BufferedReader br;

    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();}


}