/* 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<>();
		    for(int i=0;i<n;++i){
		        ll.add(Integer.parseInt(ss[i]));
		    }
		    Collections.sort(ll,Collections.reverseOrder());
		    int ans=ll.get(0);
		    for(int j=0;j<n;++j){
    		    int g0=0,k0=-1;
    		    ans=gcd(ans,ll.get(j));
    		    for(int i=j+1;i<n;++i){
    		        if(g0<gcd(ans,ll.get(i))){
    		            g0=gcd(ans,ll.get(i));
    		            k0=i;
    		        }
    		    }
    		    if(k0!=-1 && j<n-1){
    		        int temp=ll.get(j);
    		        ll.set(j,ll.get(k0-1));
    		        ll.set(k0-1,temp);
    		    }
		    }   
		   for(int i:ll)System.out.print(i+" ");
		    System.out.println();
		}
	}
}
