/*package whatever //do not write package name here */

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
    static int gcd(int a, int b) 
    { 
        if (a == 0) 
            return b; 
        return gcd(b % a, a); 
    } 
    static int findGCD(ArrayList<Integer> al, int n) 
    { 
        int result = 0; 
       for(int i=0;i<al.size();i++) {
            result = gcd(result, al.get(i)); 
  
            if(result == 1) 
            { 
               return 1; 
            } 
        } 
  
        return result; 
    } 
	public static void main (String[] args) throws java.lang.Exception{
		//code
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0) {
		    int n=sc.nextInt();
		    int a[]=new int[n];
		    ArrayList<Integer> al1=new ArrayList<>();
		    for(int i=0;i<n;i++) {
		    int x=sc.nextInt();
		    al1.add(x);
		    }
		  //  Arrays.sort(a);
		  Collections.sort(al1);
		    ArrayList<Integer> al=new ArrayList<>();
		    al.add(al1.get(n-1));
		    al1.remove(n-1);
		    int res=0;
		    int ans=0;
		    int ind=0;
		    while(al.size()!=n) {
		    for(int i=0;i<al1.size();i++) {
		        al.add(al1.get(i));
		        ans=findGCD(al,al.size());
		        if(ans>res) {
		        res=ans;
		        ind=i;
		        }
		        al.remove(al.size()-1);
		    }
		    al.add(al1.get(ind));
		    al1.remove(ind);
		  //  System.out.println(al);
		  //   System.out.println(al1);
		    res=0;
		    }
		    for(int i=0;i<al.size();i++) 
		    System.out.print(al.get(i)+" ");
		    System.out.println();
		}
	}
}