import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Solveaproblem {
    private static int gcd(int a, int b) {
        return b==0 ? a : gcd(b, a%b);
    }
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        StringBuilder ans = new StringBuilder();
        while (t-->0) {
            int n = Integer.parseInt(reader.readLine());
            StringTokenizer sToken = new StringTokenizer(reader.readLine());
            int[] arr = new int[n];
            for (int i=0; i<n; i++) arr[i] = Integer.parseInt(sToken.nextToken());
            int gcd = 0;
            boolean[] used = new boolean[n];
            for (int i=0; i<n; i++) {
                int max = 0, ind = 0;
                for (int j=0; j<n; j++) {
                    if (!used[j]) {
                        int x = gcd(gcd, arr[j]);
                        if (x>max) {
                            max = x; ind = j;
                        }
                    }
                }
                used[ind] = true;
                gcd = max;
                ans.append(arr[ind]).append(" ");
            }
            ans.append("\n");
        }
        PrintWriter writer = new PrintWriter(System.out);
        writer.print(ans);
        writer.close();
    }
}