/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
    static int gcd(int a, int b) 
    { 
      if (b == 0) 
        return a; 
      return gcd(b, a % b);  
    } 
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int t=Integer.parseInt(br.readLine());
		while(t-->0){
		    int n=Integer.parseInt(br.readLine());
		    String ss[]=br.readLine().split(" ");
		    LinkedList<Integer> ll=new LinkedList<>();
		    int a[]=new int[n+1];
		    for(int i=1;i<=n;++i){
		        a[i]=Integer.parseInt(ss[i-1]);
		        ll.add(a[i]);
		    }
		    Collections.sort(ll,Collections.reverseOrder());
		    int g=1,ct=0;
		    
		    for(int i:ll){
		        a[++ct]=i;
		    }
		    g=a[1];
		    for(int i=1;i<=n;++i){
		        int gm=0,k0=-1;
		        g=gcd(g,a[i]);
		        for(int j=i+1;j<=n;++j){
		            if(gm<gcd(g,a[j])){
                        gm=gcd(g,a[j]);
                        k0=j;
                    }
		        }
		        if(k0!=-1 && i<n){
		            int temp=a[i+1];
		            a[i+1]=a[k0];
		            a[k0]=temp;
		        }
		        
		    }
		    for(int i=1;i<=n;++i){
		        System.out.print(a[i]+" ");
		    }
		    System.out.println();
		}
	}
}
