import java.util.Arrays;
import java.util.Scanner;

public class T2 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        for (int i=0;i<n;i++) {
            mainPrint(s);
        }
    }
    public static void mainPrint(Scanner s){
        int n = s.nextInt();
        int[] a = new int[n];
        int[][] b = new int[n][2];

        for (int i=0;i<n;i++){
            a[i] = s.nextInt();
            b[i][0] = a[i];
            b[i][1] = a[i];
        }
        Arrays.sort(b,(i1,i2)->{return i1[1]-i2[1];});
        int p = b[n-1][1];
        for (int i=1;i<n;i++){
            for (int j=0;j<n-i;j++){
                b[j][1] = gcd(b[j][1],p);
            }
            Arrays.sort(b,0,n-i,(i1,i2)->{return i1[1]-i2[1];});
            p = b[n-i-1][1];
        }
        StringBuilder ans = new StringBuilder();
        ans.append(b[n-1][0]);
        for (int i = n-2;i>=0;i--){
            ans.append(' ');
            ans.append(b[i][0]);
        }
        System.out.println(ans.toString());
    }
    public static int gcd(int i1,int i2){
        int temp;
        while(i2!=0){
            temp = i2;
            i2 = i1%i2;
            i1 = temp;
        }
        return i1;
    }
}
