//package exercieses.div2_669;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class B {

    // Recursive function to return gcd of a and b
    static int gcd(int a, int b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }


    static void solve(int[] arr) {
        List<Integer> res = new ArrayList<>(arr.length);
        int max = -1;
        int maxInd = -1;
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] > max) {
                max = arr[i];
                maxInd = i;
            }
        }
        res.add(max);
        arr[maxInd] = -1;
        int last = max;
        for(int i = 1; i < arr.length; i++) {
            max = -1;
            for(int j = 0; j < arr.length; j++) {
                if(arr[j] == -1) continue;
                int gcd = gcd(arr[j],last);
                if(gcd > max) {
                    maxInd = j;
                    max = gcd;
                }
            }
            res.add(arr[maxInd]);
            last = max;//Math.min(arr[maxInd], last);
            arr[maxInd] = -1;

        }
        for(int i = 0; i < arr.length; i++) System.out.print(res.get(i) + " ");
        System.out.println();

    }

    public static void main(String[] args) {
//        System.out.println(gcd(4, 8));
        FastReader reader = new FastReader();
        int tests = reader.nextInt();
        for(int test = 1; test <= tests; test++) {
            int n = reader.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++) {
                arr[i] = reader.nextInt();
            }
            solve(arr);
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
