import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int t1=in.nextInt();
        while(t1-->0){
            int n=in.nextInt();
            int []arr=new int[n];int max=Integer.MIN_VALUE;int t=0;
            for(int i=0;i<n;i++){
                arr[i]=in.nextInt();
                if(arr[i]>max){max=arr[i];t=i;}
            }
            boolean b[]=new boolean[n];b[t]=true;
            System.out.print(max+" ");
            
            for(int a=0;a<n-1;a++){int gcd=1;t=-1;
                for(int i=0;i<n;i++){
                if(!b[i]){
                   int g1=fgcd(max,arr[i]);
                    if(g1>=gcd){
                        gcd=g1;
                        t=i;
                        }
                    }
                }
                b[t]=true;
                max=gcd;
                System.out.print(arr[t]+" ");
            }
            System.out.println();
        }
    }
    static int fgcd(int a,int b){
        return b==0 ? a :fgcd(b,a%b);
    }
}