/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
import java.util.*;
public class Main
{
    static int gcd(int i,int j)
    {
        if(j==0)
        return i;
        else
        return gcd(j,i%j);
    }
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int t=sc.nextInt();
	    while(t-->0)
	    {
	        int n=sc.nextInt();
	        int a[]=new int[n];
	        for(int i=0;i<n;i++)
	        a[i]=sc.nextInt();
	        
	        int v[]=new int[n];
	        int b[]=new int[n];
	        int x=0;
	        for(int i=0;i<n;i++)
	        {
	            int max=0;
	            int ind=0;
	            for(int j=0;j<n;j++)
	            {
	                if(v[j]==0 && gcd(x,a[j])>max)
	                {
	                    max=gcd(x,a[j]);
	                    ind=j;
	                }
	                
	            }
	            b[i]=a[ind];
	            v[ind]=1;
	            x=gcd(x,a[ind]);
	        }
	        for(int i=0;i<n;i++)
	        System.out.print(b[i]+" ");
	        System.out.println();
	    }
	
	}
}
