import java.io.*;

import java.util.*;

public class pg1 {

    public static void main(String[] args)throws IOException {
        Reader sc = new Reader();

        int t = sc.nextInt();
        while (t-- > 0) {
    int n=sc.nextInt();
    int[] arr=new int[n];
    int max_all=0;
    int ind=0;
            for (int i = 0; i <n ; i++) {
                arr[i]=sc.nextInt();
                if(arr[i]>max_all){
                    max_all=arr[i];
                    ind=i;
                }
            }

            boolean[] check=new boolean[n];
            StringBuilder sb=new StringBuilder();
            sb.append(max_all);
            int last=max_all;
            check[ind]=true;
            for (int i = 0; i <n-1 ; i++) {
                int max=0,index = 0, val ;
                for (int j = 0; j <n ; j++) {
                    val=gcd(arr[j],last);
                    if(val>max&&!check[j]){
                        max=val;
                        index=j;
                    }
                }
                last=max;
                check[index]=true;
               sb.append(" ").append(arr[index]);
            }

            System.out.println(sb.toString());
        }


    }
    public static int gcd(int a,int b){
        while (b!=0) {
            int c=a%b;
            a=b;
            b=c;
        }
        return a;
    }
    static class Reader
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        public String next()
        {
            while(!st.hasMoreTokens())
            {
                try
                {
                    st=new StringTokenizer(br.readLine());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        public int nextInt()
        {
            return Integer.parseInt(next());
        }
        public long nextLong()
        {
            return Long.parseLong(next());
        }
        public double nextDouble()
        {
            return Double.parseDouble(next());
        }
        public String nextLine()
        {
            try
            {
                return br.readLine();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
        public boolean hasNext()
        {
            String next=null;
            try
            {
                next=br.readLine();
            }
            catch(Exception e)
            {
            }
            if(next==null)
            {
                return false;
            }
            st=new StringTokenizer(next);
            return true;
        }


    }
}