
import java.io.*;


public class div2_669b {
    static int gcd(int a, int b){
        if(b==0)return a;
        else return gcd(b,a%b);
    }
    public static void main(String[] args)throws IOException {
        int m[][]=new int[1001][1001];
        for(int i=0;i<1001;i++){
            for(int j=0;j<1001;j++){

                m[i][j]=gcd(i,j);
            }
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            int n=Integer.parseInt(br.readLine());
            String line=br.readLine();
            String []s=line.trim().split(" ");
            int[]a=new int[n];

            int max=0;
            for(int i=0;i<n;i++){
                a[i]=Integer.parseInt(s[i]);
                max=Math.max(max,a[i]);
            }

            out.write(String.valueOf(max)+" ");
            int vis[]=new int[1001];
            vis[max]=1;
            for(int i=1;i<n-1;i++){
                int newmax=-1;
                int newm=-1;
                for(int j=0;j<n;j++){
                    if(m[max][a[j]]>newmax&&vis[a[j]]==0){
                        newmax=m[max][a[j]];
                        newm=a[j];
                    }

                }
                if(newm==-1||newmax==-1)break;
                if(vis[newm]!=1)
                out.write(String.valueOf(newm)+" ");
                max=newmax;
                vis[newm]=1;
            }
            for(int i=0;i<n;i++){
                if(vis[a[i]]==0)out.write(String.valueOf(a[i])+" ");
            }
            out.newLine();






        }
        out.flush();
    }
}
