import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class B
{
	
	static int gcd(int a, int b) 
	{ 
	    if (a == 0) 
	        return b; 
	    return gcd(b % a, a); 
	} 
	
	static class Node implements Comparable<Node>
	{
		static int cur = 1;
		int val = 0;
		boolean taken=false;
		
		Node(int val)
		{
			this.val = val;
		}

		@Override
		public int compareTo(Node o)
		{
			if(this.taken)
				return 1;
			else if(o.taken)
				return -1;
			
			int u = gcd(cur,this.val);
			int v = gcd(cur,o.val);
			
			//System.out.println("cur="+cur+" this="+this.val+", o="+o.val+": "+u+","+v);
			
			if(u!=v)
			{
				if(u>v)
					return -1;
				else
					return 1;
			}
			else
				return o.val-this.val;
				
		}
		
		public String toString()
		{
			return ""+this.val;
		}
		
	}

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		
		int t = sc.nextInt();
		while(t-->0)
		{
			int n = sc.nextInt();
			
			Node[] a = new Node[n];
			
			for(int i=0;i<n;i++)
			{
				a[i] = new Node(sc.nextInt());
			}
			ArrayList<Integer> b = new ArrayList<Integer>();
			
			for(int i=0;i<n;i++)
			{
				Arrays.sort(a);
				
				b.add(a[0].val);
				Node.cur = a[0].val;
				a[0].taken=true;
				
			}
			
			for(int e:b)
			{
				System.out.print(e+" ");
			}
			System.out.println();
			
			Node.cur = 0;
			
		}
		
	}
	
}
