import java.util.*;  
 
public class a {
    static char[] count;
    // static int[] count1;
    static int[] arr;
    static char[] c;
    static int[] darr;
    // static int x;
    static int maxl;
    // static long minl;
    static long mod = 998244353l; 
    static int n;
    static long a;
    static long b;
    static long x;
    static long y;
    static long m;
    static long k;
    static String str;
    static Set<Integer> set;
    // static Map<Long,Long> map;
    static StringBuilder sb;

    public static int gcd(int a, int b){ 

        if (a == 0) 
            return b; 

        return gcd(b % a, a); 
    } 
    
    // public static int log(long n){
    //     if(n == 0)
    //         return 0;
            
    //     if(n == 1)
    //         return 0;
    //     if(n == 2)
    //         return 1;
    //     double num = Math.log(n);
    //     double den = Math.log(2);
    //     if(den == 0)
    //         return 0;
    //     return (int)(num/den);
    // }

    // public static void swap(int i,int j){
    //     long temp = arr[j];
    //     arr[j] = arr[i];
    //     arr[i] = temp;
    // }
    

    
    public static void solve(){

        
        set = new HashSet<>();
        set.add(maxl);
        int prev = arr[maxl];
        darr = new int[n];
        darr[0] = prev;
        int index = 0;
        int mx = 1;

        for(int j = 1 ; j < n ; j++){
            mx = 1;
            index = 0;
            for(int i = 0 ; i < n ; i++){
                if((!set.contains(i))){
                    int k = gcd(prev,arr[i]);
                    if(k >= mx){
                        mx = k;
                        index = i;
                    }
                }
            }
            set.add(index);
            prev = mx;
            darr[j] = arr[index];
        }

        sb = new StringBuilder();
        for(int i = 0 ; i < n  ; i++){
            sb.append(darr[i]+" ");
        }
        System.out.println(sb);

    }
    
  public static void main(String[] args) {
      
    Scanner sc = new Scanner(System.in);  
        
        int t = sc.nextInt();
        while(t > 0){
            // set = new HashSet<>();
            // map = new HashMap<>();
            
           
            // long x = sc.nextLong();
            
            // maxl = Integer.MIN_VALUE;
            
            
            n = sc.nextInt();
            // m = sc.nextLong();

            // k = sc.nextLong();
            // c = sc.next().toCharArray();

            // k = sc.nextInt();
            arr = new int[n];
            // // // darr = new char[n];
            int mx = -1;
            for(int i = 0; i < n ; i++){
                arr[i] = sc.nextInt(); 
                if(mx < arr[i]){
                    mx = arr[i];
                    maxl = i;
                }

            }
                        
            

            // darr = new int[m];
            // for(int i = 0; i < m ; i++){
            //     darr[i] = sc.nextInt();
            // }
            // boolean flag = false;
            // str = sc.next();
            
            // x = sc.nextInt();
            
            
            
            //System.out.println(solve()?"Yes":"No");
            solve();
            //System.out.println(solve());
            // sb = new StringBuilder();
            // sb.append(str);
    
            t -= 1;
        }
        

       
  }
}