
import java.util.*;
import java.lang.*;
public class Big_Vollo
{
    //m>n
    static int gcd(int n,int m){
        if(n==1||n==0){
            return m;
        }
        if(m==1||m==0){
            return n;
        }
        if(n>m){
            return gcd(m%n,m);
        }
        else
            return gcd(n%m,n);

    }
    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        int test=in.nextInt();

        while(test-- > 0){
            int n=in.nextInt();
            int arr[]=new int[n];
            for (int i=0;i<n;i++){
                arr[i]=in.nextInt();
            }
            int d=0;
            boolean visited [] =new boolean[n];
            int ans[]=new int[n];
            int max=0;

            for (int i=0;i<n;i++){
                int ind=0;
                for (int j=0;j<n;j++){
                    if(!visited[j]){
                        if(gcd(d,arr[j])>max)
                        max=gcd(d,arr[i]);
                        ind=j;
                    }
                }
                d=max;
                ans[i]=arr[ind];
                visited[ind]=true;
            }
            for (int i=0;i<n;i++){
                System.out.print(ans[i]+" ");
            }
            System.out.println();


        }
    }
}
