import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class B {
    static Utility.FastReader fr = new Utility.FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        long timeStart = System.nanoTime();
        int tt = fr.nextInt();
        while (tt-- > 0) {
            int n=fr.nextInt();
            int[] arr=new int[n];
            boolean[] selected=new boolean[n];
            for(int i=0; i<n; ++i) arr[i]=fr.nextInt();
            ArrayList<Integer> ans=new ArrayList<>();
            int mx=Integer.MIN_VALUE;
            for(int i=0; i<n; ++i){
                mx=Math.max(mx, arr[i]);
            }
            ans.add(mx);
            for(int i=0; i<n; ++i){
                if(arr[i]==mx){
                    selected[i]=true;
                    break;
                }
            }
            for(int i=0; i<n-1; ++i){
                int mxG=1;
                int g=ans.get(0);
                for(int j=0; j<ans.size(); ++j)
                    g=Utility.gcd(g, ans.get(j));
                int idx=-1;
                for(int j=0; j<n; ++j){
                    if(!selected[j]){
                        if(Utility.gcd(g, arr[j])>=mxG){
                            mxG=Utility.gcd(g, arr[j]);
                            idx=j;
                        }
                    }
                }
                if(idx==-1){
                    for(int j=0; j<n; ++j){
                        if(!selected[j]){
                            idx=j;
                            break;
                        }
                    }
                }
                selected[idx]=true;
                ans.add(arr[idx]);
            }
            for(int x: ans){
                out.print(x + " ");
            }
            out.println();
        }
        long timeEnd = System.nanoTime();
        System.err.println("Completed in :" + ((double) (timeEnd - timeStart) / 1000000) + " miliseconds");
        out.close();
    }

    static Supplier<List<String>> readListString = () -> Arrays.stream(fr.nextLine().split(" "))
            .collect(Collectors.toList());

    static Supplier<List<Integer>> readListInteger = () -> readListString.get().stream().map(x -> Integer.parseInt(x))
            .collect(Collectors.toList());

    static Supplier<List<Double>> readListDouble = () -> readListString.get().stream().map(x -> Double.parseDouble(x))
            .collect(Collectors.toList());

    static Supplier<int[]> readArrInteger = () -> readListString.get().stream().mapToInt(i -> Integer.parseInt(i)).toArray();

    static Supplier<double[]> readArrDouble = () -> readListString.get().stream().mapToDouble(i -> Double.parseDouble(i)).toArray();

    static Supplier<String[]> readArrString = () -> (String[]) readListString.get().toArray();

    static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }

    static class Utility {

        static int getMaxInt(Integer... o) {
            int res = Integer.MIN_VALUE;
            for (int x : o) res = Math.max(x, res);
            return res;
        }

        static int getMinInt(Integer... o) {
            int res = Integer.MAX_VALUE;
            for (int x : o) res = Math.min(x, res);
            return res;
        }

        static long getMaxLong(Long... o) {
            long res = Long.MIN_VALUE;
            for (long x : o) res = Math.max(x, res);
            return res;
        }

        static long getMinLong(Long... o) {
            long res = Long.MAX_VALUE;
            for (long x : o) res = Math.min(x, res);
            return res;
        }


        static void sort(int[] a) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int i : a) list.add(i);
            Collections.sort(list);
            for (int i = 0; i < a.length; i++) a[i] = list.get(i);
        }

        static void shuffleArray(long[] arr) {
            int n = arr.length;
            Random rnd = new Random();
            for (int i = 0; i < n; ++i) {
                long tmp = arr[i];
                int randomPos = i + rnd.nextInt(n - i);
                arr[i] = arr[randomPos];
                arr[randomPos] = tmp;
            }
        }

        static void shuffleArray(int[] arr) {
            int n = arr.length;
            Random rnd = new Random();
            for (int i = 0; i < n; ++i) {
                int tmp = arr[i];
                int randomPos = i + rnd.nextInt(n - i);
                arr[i] = arr[randomPos];
                arr[randomPos] = tmp;
            }
        }

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

        static long gcd(long a, long b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }

        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;
            }
        }
    }
}

