import java.util.Scanner;

public class B2
{

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

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		
		int t = sc.nextInt();
		
		while(t-->0)
		{
			int n = sc.nextInt();
			int[] arr = new int[n];
			
			for(int i=0;i<n;i++)
				arr[i] = sc.nextInt();
			
			int[] b = new int[n];
			
			boolean[] vis = new boolean[n];
			
			int cur = 0;
			int mg = 1;
			int m = 1;
			int mi = 0;
			for(int i=0;i<n;i++)
			{
				
				
				for(int j=0;j<n;j++)
				{
					int tg = gcd(cur,arr[j]);
					//System.out.println(cur+","+arr[j]+"="+tg);
					if(!vis[j] && tg>mg)
					{
						mi = j;
						m = arr[j];
						mg = tg;
					}
					else if(!vis[j] && tg==mg && arr[j]>m)
					{
						mi = j;
						m = arr[j];
					}
				}
				vis[mi] = true;
				b[i] = m;
				cur = gcd(cur,m);
				mg = 1;
				m = 1;
				mi = 0;
			}
			
			for(int e:b)
			{
				System.out.print(e+" ");
			}
			System.out.println();
			
			
		}
		
		
	}
	
}
