import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Solution {

    private static final String NO = "NO";
    private static final String YES = "YES";
    public static BufferedWriter bw;
    public static FR sc;

    public static void main(String[] args) throws Exception {
        FR sc = new FR();
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = sc.i();
        while (t > 0) {
            t--;
            int n = sc.i();
            int[] a = sc.ia(n);
            int[] b = solve(n, a);
            for (int ans : b) {
                bw.write(ans + " ");
            }
            bw.write("\n");
        }
        bw.close();
    }


    private static int[] solve(int n, int[] a) {
        int[] b = new int[n];
        int[] ans = new int[n];
        int max = 0;
        for (int x : a) {
            if (x > max) {
                max = x;
            }
        }
        int gcd = max;
        for (int i = 0; i < n; i++) {
            b[i] = gcd(gcd, a[i]);
        }
        int count = 0;
        while (count < n) {
            for (int i = 0; i < n; i++) {
                if (b[i] == gcd) {
                    ans[count++] = a[i];
                    b[i] = -1;
                }
            }
            for (int i = 0; i < n; i++) {
                if(b[i]!=-1)
                    b[i] = gcd(gcd, a[i]);
            }
            gcd=-1;
            for (int i = 0; i < n; i++) {
                if (gcd < b[i]) gcd = b[i];
            }
        }
        return ans;
    }

    public static int gcd(int x, int y) {
        if (y == 0)
            return x;
        return gcd(y, x % y);
    }


}



class FR {
    BufferedReader br;
    StringTokenizer st;

    public FR() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public String s() {
        if (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {

            }
        }
        return st.nextToken();
    }

    public int i() {
        return Integer.parseInt(s());
    }

    public int[] ia(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = i();
        }
        return a;
    }

    public long[] la(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = l();
        }
        return a;
    }

    public String[] sa(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++) {
            a[i] = s();
        }
        return a;
    }

    public long l() {
        return Long.parseLong(s());
    }
}