import java.util.*;

public class Solution {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int t=in.nextInt();
        while(t-->0){
            int n=in.nextInt(),max=0;
            int[] a=new int[n];
            HashMap<Integer,Integer> hm=new HashMap<>();
            for(int i=0;i<n;i++){
                a[i]=in.nextInt();
                max=Math.max(a[i],max);
                if(!hm.containsKey(a[i]))
                    hm.put(a[i], 0);
                hm.put(a[i],hm.get(a[i])+1);
            }
            //System.out.println("hm is: ->"+hm);
            int gcd =max;
            ArrayList<Integer> divs;

            while(gcd!=1){
                divs = getDivs(gcd);
                //System.out.println("div is : "+divs);
                gcd=1;
                int found=-1;
                outer: for(int i=0;i<divs.size();i++){
                    for(int x:hm.keySet()){
                    if(x%divs.get(divs.size()-1-i)==0){
                        gcd=divs.get(divs.size()-1-i);
                        max=x;
                    
                        hm.put(max,hm.get(max)-1);
                        
                        if(hm.get(max)==0)
                            hm.remove(max);
                        found=1;
                        break outer;
                    }
                }
                }
                if(found==1)
                    System.out.print(max+" ");
                else
                    gcd=1;


            }
            //System.out.println("hm is: ->"+hm);
            for(int x:hm.keySet()){
                for(int i=0;i<hm.get(x);i++)
                    System.out.print(x+" ");
            }
            System.out.println();
            

        }

    }
    public static ArrayList<Integer> getDivs(int x){
        ArrayList<Integer> allDiv=new ArrayList<>();
        for(int div=1;div<=Math.sqrt(x);div++){
            if(x%div==0){
                allDiv.add(div);
                if(x/div!=div){
                    allDiv.add(x/div);
                }
            }
        }
        Collections.sort(allDiv);
        return allDiv;
    }

}