
import java.io.*;
import java.util.*;
public class Main {
	static BufferedReader br;
	static int cin() throws Exception
	{
		return Integer.valueOf(br.readLine());
	}
	static int[] split() throws Exception
	{
		String[] cmd=br.readLine().split(" ");
		int[] ans=new int[cmd.length];
		for(int i=0;i<cmd.length;i++)
		{
			ans[i]=Integer.valueOf(cmd[i]);
		}
		return ans;
	}
	static long[] splitL() throws IOException
	{
		String[] cmd=br.readLine().split(" ");
		long[] ans=new long[cmd.length];
		for(int i=0;i<cmd.length;i++)
		{
			ans[i]=Long.valueOf(cmd[i]);
		}
		return ans;
	}
	static int sum(long n)
	{
		int ans=0;
		while(n!=0)
		{
			ans=ans+(int)(n%10);
			n=n/10;
		}
		return ans;
	}
	static int gcd(int x,int y)
	{
		if(x==0)
			return y;
		if(y==0)
			return x;
		if(x>y)
			return gcd(x%y,y);
		else
			return gcd(x,y%x);
	}
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		br=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
		int cases=cin();
		while(cases!=0)
		{
			cases--;
			int n=cin();
			int[]arr=split();
			int[]vis=new int[n];
			int g=0;
			ArrayList<Integer> ans=new ArrayList<>();
			while(ans.size()!=n)
			{
				int x=-1;
				int mx=0;
				for(int i=0;i<n;i++)
				{
					if(vis[i]==1)
						continue;
					int y=gcd(g,arr[i]);
					if(y>=mx)
					{
						x=i;
						mx=y;
					}
				}
				if(x==-1)
					break;
				ans.add(arr[x]);
				vis[x]=1;
				g=mx;
			}
			for(int i=0;i<n;i++)
			{
				System.out.print(ans.get(i)+" ");
			}
			System.out.println();
		}
	}
}
