import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;

public class B_BigVova {
	static int n;

	public static void main(String[] args) throws NumberFormatException, IOException 
	{
		BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);
		String[] in;
		int tt = parse(scan.readLine());
		
		for(int t = 0; t < tt; t++)
		{
			n = parse(scan.readLine());
			
			ArrayList<Integer> nums = new ArrayList<Integer>();
			in = scan.readLine().split(" ");
			for(int i = 0; i < n; i++)
				nums.add(parse(in[i]));
			
			Collections.sort(nums);
			
			int maxGCD = nums.get(n-1);
			nums.remove(n-1);
			out.print(maxGCD);
			
			printRelated(maxGCD, nums, out);
			
			while(!nums.isEmpty())
			{
				maxGCD = getNewGCD(maxGCD, nums);
				printRelated(maxGCD, nums, out);
			}
			
			out.println();
			out.flush();
		}
	}

	public static int getNewGCD(int maxGCD, ArrayList<Integer> nums) 
	{
		int max = Integer.MIN_VALUE, temp;
		
		for(int i : nums)
		{
			temp = gcd(Math.max(maxGCD, i), Math.min(maxGCD, i));
			
			if(temp > max)
			{
				max = temp;
			}
		}
		
		return max;
	}

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

	public static void printRelated(int maxGCD, ArrayList<Integer> nums, PrintWriter out) 
	{
		for(int i = nums.size() - 1; i >= 0; i--)
		{
			if(maxGCD > nums.get(i)) continue;
			
			if(nums.get(i) % maxGCD == 0)
				out.print(" " + nums.remove(i));
		}
	}

	public static int parse(String num)
	{
		return Integer.parseInt(num);
	}
}
