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

public class B {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i=0 ; i<n ; i++){
            int size = in.nextInt();
            int[] nums = new int[size];
            int[] c = new int[2000];
            int[] b = new int[2000];
            for(int j=0 ; j<size ; j++){
                nums[j] = in.nextInt();
                c[j] = 1;
            }
            Arrays.sort(nums);
            int lgg = nums[size-1];
            int pos = 0;
            int tmp2 = 0;
            b[pos] = nums[size-1];
            c[size-1] = 0;
            while(lgg > 1 && pos < size){
                int gg = 0;
                pos++;
                for(int k=0 ; k<size ; k++){
                    if(c[k]>0){
                        int tmp1 = GCD(lgg,nums[k]);
                        if(tmp1 > gg){
                            gg = tmp1;
                            b[pos] = nums[k];
                            tmp2 = k;
                        }
                    }
                    c[tmp2] = 0;
                    lgg = gg;
                }
            }
            for(int j=0 ; j<size ; j++) if(c[j]>0)  b[++pos] = nums[j];
            for(int j=0 ; j<size ; j++) System.out.print(b[j]+" ");
            System.out.print("\n");
        }
        in.close();
    }
    private static int GCD(int a , int b ) {
        return b==0? a : GCD(b,a%b);
    }
}