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


public class Main {
    public static void main(String[] args) throws IOException {
        try (BufferedReader input = new BufferedReader(new InputStreamReader(System.in))) {
            try (PrintWriter output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)))) {
                int t = Integer.parseInt(input.readLine());
                for (int q = 0; q < t; q++) {
                    int n = Integer.parseInt(input.readLine());
                    TreeSet<Integer> arr = Arrays.stream(input.readLine().split("\\p{javaWhitespace}"))
                            .filter(it -> !it.isEmpty())
                            .map(Integer::parseInt)
                            .collect(Collectors.toCollection(TreeSet::new));
                    int gcd = arr.pollLast();
                    output.print(gcd);
                    while (!arr.isEmpty()) {
                        output.print(' ');
                        int newGcd = 0;
                        Integer nextEl = 0;
                        for (Integer el : arr) {
                            if (newGcd < gcd(gcd, el)) {
                                nextEl = el;
                                newGcd = gcd(gcd, el);
                            } else if (newGcd == gcd(gcd, el) && el.toString().compareTo(nextEl.toString()) > 0) {
                                nextEl = el;
                            }
                        }
                        arr.remove(nextEl);
                        output.print(nextEl);
                        gcd = newGcd;
                    }
                    output.println();
                }
            }
        }
    }

    private static int gcd(int a, int b) {
        return b != 0 ? gcd(b, a % b) : a;
    }
}
