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

public class Codeforces
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int t=Integer.parseInt(bu.readLine());
        while(t-->0)
        {
            int n=Integer.parseInt(bu.readLine());
            String s[]=bu.readLine().split(" ");
            int a[]=new int[n],i,ans[]=new int[n],max=-1,maxp=-1;
            for(i=0;i<n;i++)
            {
                a[i]=Integer.parseInt(s[i]);
                if(a[i]>max) {max=a[i]; maxp=i;}
            }

            ans[0]=max; a[maxp]=-1;
            int j,g=ans[0];
            for(i=1;i<n;i++)
            {
                max=0; maxp=-1;
                for(j=0;j<n;j++)
                {
                    if(a[j]==-1) continue;
                    int cr=gcd(g,a[j]);
                    if(cr>max)
                    {
                        max=cr;
                        maxp=j;
                    }
                }
                ans[i]=a[maxp];
                a[maxp]=-1;
                g=max;
            }
            for(i=0;i<n;i++)
                sb.append(ans[i]+" ");
            sb.append("\n");
        }
        System.out.print(sb);
    }

    static int gcd(int a,int b)
    {
        int t;
        while(b!=0)
        {
            t=b;
            b=a%b;
            a=t;
        }
        return a;
    }
}