

import java.io.*;
import java.util.*;

public class B {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        int tt = in.nextInt();

        while(tt-- > 0){
            int n = in.nextInt();
            int[] a = in.readArray(n);
            boolean[] used = new boolean[n];
            ArrayList<Integer> b = new ArrayList<>();
            int prevGCD = 0;
            for(int i = 0; i < n; i++){
                int bestIdx = -1;
                int best = 0;
                for(int j = 0; j <n; j++){
                    if(used[j]){
                        continue;
                    }
                    int gcd = gcd(a[j], prevGCD);
                    if(gcd >= best){
                        best = gcd;
                        bestIdx = j;
                    }
                }
                used[bestIdx] = true;
                b.add(a[bestIdx]);
                prevGCD = best;
            }
            for(int num : b){
                System.out.print(num +" ");
            }
            System.out.println();
        }
    }
    static int gcd(int a, int b){
        if(b == 0){
            return a;
        }
        return gcd(b, a % b);
    }

    static void debug(int[] a){
        for(int n: a){
            System.out.print(n + " ");
        }
        System.out.println();
    }
    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;
        }
        int[] readArray(int n)
        {
            int[] a = new int[(int)n];
            for(int i = 0; i < n; i++)
            {
                a[i] = nextInt();
            }
            return a;
        }
    }
}
