import java.util.*;
import java.io.*;
 
public class Solution{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		int numTestCases=Integer.parseInt(input[0]);
		while(numTestCases-->0){
		    input=br.readLine().trim().split(" ");
		    int n=Integer.parseInt(input[0]);
    		int[] arr=new int[n];
    		input=br.readLine().trim().split(" ");
    		for(int i=0;i<n;i++){
    		    arr[i]=Integer.parseInt(input[i]);
    		}
    		printNewArray(arr);
		}
		
		out.flush();
		out.close();
	}
	public static void printNewArray(int[] arr)
	{
	    int n = arr.length;
	    ArrayList<Integer> ans = new ArrayList<>();
	    int max = Integer.MIN_VALUE;
	    for(int i = 0; i < n; i++){
	        max = Math.max(max, arr[i]);
	    }
	    ans.add(max);
	    int prevGCD = max;
	    boolean[] isRemoved = new boolean[n];
	    for(int i = 0; i < n; i++){
	        if(arr[i] == max)
	        {
	            isRemoved[i] = true;
	            break;
	        }
	    }
	    for(int j = 1; j < n; j++){
	        int maxGCD = Integer.MIN_VALUE;
	        int maxGCDIndex = -1;
	        for(int i = 0; i < n; i++){
	            if(isRemoved[i] == false)
	            {
	                int gcd = gcd(prevGCD, arr[i]);
	                if(gcd > maxGCD)
	                {
	                    maxGCD = gcd;
	                    maxGCDIndex = i;
	                }
	            }
	        }
	        isRemoved[maxGCDIndex] = true;
	        ans.add(arr[maxGCDIndex]);
	        prevGCD = maxGCD;
	    }
	    for(int i = 0; i < n; i++){
	        out.print(ans.get(i) + " ");
	    }
	    out.println();
	}
	public static int gcd(int a, int b)
	{
	    if(a == 0){
	        return b;
	    }
	    if(b == 0){
	        return a;
	    }
	    return gcd(b % a, a);
	}
}