import java.util.*;
public class p1407B {
    public static void main(String[] args) {
        Scanner scn=new Scanner(System.in);
        int t=scn.nextInt();
        for(int i1=0;i1<t;i1++){
            int n=scn.nextInt();
            ArrayList<Integer> arr1=new ArrayList<Integer>();
            int[] arr=new int[n];
            int max=0;
            for(int i=0;i<n;i++){
                arr[i]=scn.nextInt();
            }
            for(int i=0;i<n;i++){
                if(arr[i]>arr[max]){
                    max=i;
                }
            }
            arr1.add(arr[max]);
            int gcd=arr[max];
            arr[max]=-1;
            
            while(arr1.size()<n){
                int gcd1=-1;
                int idx=-1;
                for(int i=0;i<n;i++){
                    if(arr[i]==-1){
                        continue;
                    }
                    else{
                        int n1=gcd(arr[i],gcd);
                        
                        if(n1>gcd1){
                            gcd1=n1;
                            idx=i;
                        }
                    }
                }
                
                
                arr1.add(arr[idx]);
                arr[idx]=-1;
            }
            for(int i=0;i<arr1.size();i++){
                System.out.print(arr1.get(i)+" ");
            }
            System.out.println();

        }
        
    }
    public static int gcd(int a,int b){
        if(b==0){
            return(a);
        }
        if(a>b){
            int m=a%b;
            a=b;
            b=m;
            return(gcd(a,b));
        }
        else{
            int m=b%a;
            b=m;
            return(gcd(a,b));
        }
    }
}
