// package Div2_669;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;

public class ProblemB {
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        StringBuilder print=new StringBuilder();
        int test=Integer.parseInt(br.readLine());
        while(test--!=0){
            int n=Integer.parseInt(br.readLine());
            int a[]=new int[n+1];
            int max=0;
            int pos=-1;
            st=new StringTokenizer(br.readLine());
            HashSet<Integer> ind=new HashSet<>();
            for(int i=1;i<=n;i++){
                ind.add(i);
                a[i]=Integer.parseInt(st.nextToken());
                if(a[i]>max){
                    max=a[i];
                    pos=i;
                }
            }
            ArrayList<Integer> ans=new ArrayList<>();
            ans.add(a[pos]);
            ind.remove(pos);
            int g=a[pos];
            for(int i=2;i<=n;i++){
                int maxg=0;
                pos=-1;
                for(int j:ind){
                    int gcd=gcd(g,a[j]);
                    if(gcd>maxg){
                        maxg=gcd;
                        pos=j;
                    }
                }
                ind.remove(pos);
                g=maxg;
                ans.add(a[pos]);
            }
            for(int i=0;i<ans.size();i++){
                print.append(ans.get(i)).append(" ");
            }
            print.append("\n");
        }
        System.out.print(print.toString());
    }
    public static int gcd(int a,int b){
        if(b==0){
            return a;
        }
        return gcd(b,a%b);
    }
}
