import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
// import java.util.HashMap;
// import java.util.HashSet;



public class ans2{
    static int mod = (int)10e8;

    public static int lower(int arr[],int key){
        int low = 0;
        int high = arr.length-1;
        while(low < high){
            int mid = low + (high - low)/2;
            if(arr[mid] >= key){
                high = mid;
            }
            else{
                low = mid+1;
            }
        }
        return low;
    }

    public static int upper(int arr[],int key){
        int low = 0;
        int high = arr.length-1;
        while(low < high){
            int mid = low + (high - low+1)/2;
            if(arr[mid] <= key){
                low = mid;
            }
            else{
                high = mid-1;
            }
        }
        return low;
    }

    public static ArrayList<Integer> ppf(int n) 
    { 
        ArrayList<Integer> list = new ArrayList<>();
        while (n%2==0) 
        { 
            // System.out.print(2 + " "); 
            list.add(2);
            n /= 2; 
        } 
  
    
        for (int i = 3; i <= Math.sqrt(n); i+= 2) 
        { 
            
            while (n%i == 0) 
            { 
                // System.out.print(i + " "); 
                list.add(i);
                n /= i; 
            } 
        } 
  
        if (n > 2) 
            list.add(n);

        return list;
    } 

    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);

        int test = scn.nextInt();
        StringBuilder sb = new StringBuilder();
        while(test-- > 0){
            int n = scn.nextInt();
            int arr[] = new int[n];
            int max = 0;
            for(int i = 0 ; i < n ; i++){
                arr[i] = scn.nextInt();
                max = Math.max(arr[i] , max);
            }
            ArrayList<Integer> pf = ppf(max);

            int help[][] = new int[n][2];

            for(int i = 0 ; i < n ; i++){
                int num = 1;
                for(int j : pf){
                    if(arr[i]%j == 0){
                        num *= j;
                    }
                }
                help[i][0] = num;
                help[i][1] = arr[i];
            }

            Arrays.sort(help , (a , b)->{
                if(a[0] == b[0]) return b[1] - a[1];
                else return b[0] - a[0];
            });

            for(int i[] : help){
                sb.append(i[1] + " ");
            }

            sb.append("\n");

        }
        System.out.println(sb);
    }
}