import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class BigVova {
    public static void main(String[] args) {
        FastReader input=new FastReader();
        int t=input.nextInt();
        while(t-->0)
        {
            int n=input.nextInt();
            LinkedList<Integer> a=new LinkedList<>();
            int max=Integer.MIN_VALUE;
            for(int i=0;i<n;i++)
            {
                int v=input.nextInt();
                a.add(v);
                max=Math.max(max,v);
            }
            int g=max;
            StringBuilder sb=new StringBuilder("");
            while(a.size()>0)
            {
                int index=0;
                int m=Integer.MIN_VALUE;
                for(int i=0;i<a.size();i++)
                {
                    int gcd=gcd(g,a.get(i));
                    if(m<gcd)
                    {
                        m=gcd;
                        index=i;
                    }
                }
                g=m;
                sb.append(a.get(index)+" ");
                a.remove(index);
                if(g==1)
                {
                    break;
                }
            }
            for(int i=0;i<a.size();i++)
            {
                sb.append(a.get(i)+" ");
            }
            System.out.println(sb.toString());
        }
    }
    public static int gcd(int a,int b)
    {
        if(a==0)
        {
            return a;
        }
        if(b==0)
        {
            return b;
        }
        if(a<b)
        {
            return gcd(a,b-a);
        }
        else if(a>b)
        {
            return gcd(a-b,b);
        }
        else
        {
            return a;
        }
    }
    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;
        }
    }
}
