import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class BigVova {
    public static int gcd(int a, int b) 
    { 
        if (a == 0) 
            return b; 
          
        return gcd(b%a, a); 
    }
    static class Compare implements Comparator<Integer>{
        int max;
        Compare(int max){
            this.max=max;
        }
        @Override
        public int compare(Integer a, Integer b) {
            int gcd1=gcd(a, max);
            int gcd2=gcd(b,max);
            return Integer.compare(gcd2, gcd1);
        }

    }
    
    public static void main(String[] args) throws IOException{
        BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(r.readLine());
        while(t-->0){
            int n=Integer.parseInt(r.readLine());
            String[]data=r.readLine().split(" ");
            ArrayList <Integer>list=new ArrayList<>();
            for(int i=0;i<n;i++)
                list.add(Integer.parseInt(data[i]));
            int max=0;
            for(int i:list)
                max=Math.max(max, i);
            Collections.sort(list,new Compare(max));
            for(int i:list)
                System.out.print(i+" ");
            System.out.println();
        }   
    } 
}
