import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

//        BufferedReader br = new BufferedReader(
//                new InputStreamReader(System.in));
//        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
//        log.flush();


public class Main {
    public static int gcd(int a, int b) {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

    public static void main(String[] args) throws java.lang.Exception {
        Scanner sc = new Scanner(System.in);
        int test = sc.nextInt();
        for (int t = 0; t < test; t++) {
            int n = sc.nextInt();
            int[] a = new int[n];
            HashSet<Integer> used = new HashSet<>();
            int pointer = Integer.MIN_VALUE;
            for (int i = 0; i < n; i++) {
                a[i] = sc.nextInt();
                if (a[i] > pointer) {
                    pointer = a[i];
                    used.clear();
                    used.add(i);
                }
            }
            System.out.print(pointer + " ");
            for (int i = 1; i < n; i++) {
                int maxGCD = Integer.MIN_VALUE, maxIndex = -1;
                for (int j = 0; j < n; j++) {
                    if (used.contains(j)) continue;
                    int currentGCD = gcd(pointer, a[j]);
                    if (currentGCD > maxGCD) {
                        maxGCD = currentGCD;
                        maxIndex = j;
                    }
                }
                used.add(maxIndex);
                pointer = maxGCD;
                System.out.print(a[maxIndex] + " ");
            }
            System.out.println();
        }
    }
}

