import com.sun.security.jgss.GSSUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public 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;
        }
    }

    public static FastReader r=new FastReader();
    public static final int INF=1000000005;
    public static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        if (b == 0)
            return a;
        if (a == b)
            return a;
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }
    public static void solve(){
        int n=r.nextInt();
        int []a=new int[n];
        int maxx=0;
        int id=0;
        for(int i=0;i<n;++i){
            a[i]=r.nextInt();
            if(maxx<a[i]){
                maxx=a[i];
                id=i;
            }
        }
        int []b=new int[n];
        boolean []used=new boolean[n];
        for(int i=0;i<n;++i) used[i]=false;
        b[0]=maxx;
        used[id]=true;
        for(int i=1;i<n;++i){
            int maxGcd=0;
            int idJ=-1;
            for(int j=0;j<n;++j){
                if(used[j]) continue;
                int gcdJ=gcd(maxx,a[j]);
                if(maxGcd<gcdJ){
                    maxGcd=gcdJ;
                    idJ=j;
                }
            }
            if(idJ==-1){
                break;
            }
            maxx=gcd(maxx,a[idJ]);
            b[i]=a[idJ];    
            used[idJ]=true;
        }
        for(int i=0;i<n;++i) System.out.print(b[i]+" ");
        System.out.println();
    }

    public static void main(String[] args) {
        int t=r.nextInt();
        while(t-->0) solve();
    }
}
