import java.util.*;

public class A {
    public static int gcd(int a, int b) {
        if (a == 0)
            return b;

        return gcd(b % a, a);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int testCases = sc.nextInt();
        first: for (int t = 0; t < testCases; t++) {
            int n = sc.nextInt();
            Integer arr[] = new Integer[n];
            int count[] = new int[1003];

            int ind = 0;
            for (int i = 0; i < n; i++) {
                int cur = sc.nextInt();
                if (count[cur] == 0) {
                    arr[ind++] = cur;
                }
                count[cur]++;
            }

            if (n == 1) {
                sb.append(arr[0]).append("\n");
                continue;
            }

            n = ind;
            arr = Arrays.copyOf(arr, n);

            Arrays.sort(arr, Collections.reverseOrder());

            int prevPos = 0;
            while (count[arr[prevPos]] != 0) {
                sb.append(arr[prevPos] + " ");
                count[arr[prevPos]]--;
            }

            boolean done[] = new boolean[n];

            int prev = arr[prevPos];
            for (int i = 1; i < n; i++) {
                int max = Integer.MIN_VALUE;
                int index = -1;
                for (int j = 1; j < n; j++) {
                    if (done[j]) {
                        continue;
                    }
                    int gcd = gcd(prev, arr[j]);
                    if (max < gcd) {
                        max = gcd;
                        index = j;
                    }
                }

                if (index == -1) {
                    break;
                }

                if (index != -1 && max == 1) {
                    for (int j = 1; j < n; j++) {
                        if (!done[j]) {
                            sb.append(arr[j] + " ");
                        }
                    }
                    sb.append("\n");
                    continue first;
                }

                prevPos = index;
                prev = max;
                done[index] = true;
                while (count[arr[index]] != 0) {
                    sb.append(arr[index] + " ");
                    count[arr[index]]--;
                }
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}