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

public class Tester
{
    public static int gcd(int a, int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
	public static void main (String[] args) throws Exception
	{
        Reader.init();
        int t = Reader.nextInt();
        while(t-->0){
            int n = Reader.nextInt();
            int max=0;
            int[] arr = new int[n];
            boolean[] v = new boolean[n];
            List<Integer> result = new ArrayList<Integer>();
            for(int i=0;i<n;i++){
                arr[i]=Reader.nextInt();
                if(arr[i]>arr[max]){
                    max=i;
                }
            }
            result.add(arr[max]);
            v[max]=true;
            for(int i=0;i<n-1;i++){
                int ele = result.get(result.size()-1);
                int mg, idx;
                mg = idx = 0;
                for(int j=0;j<n;j++){
                    int gg = gcd(arr[j],ele);
                    if(!v[j] && mg<gg){
                        mg = gg;
                        idx = j;
                    }
                }
                result.add(arr[idx]);
                v[idx]=true;
            }
            for(int i:result)System.out.print(i+" ");
            System.out.println();
        }
	}
}

class Reader {
    private static BufferedReader br;
    private static StringTokenizer stk;
    public static void init() {
        br = new BufferedReader(new InputStreamReader(System.in));
        stk = new StringTokenizer("");
    }
    public static String next() throws IOException {
        while(!stk.hasMoreTokens()){
            stk = new StringTokenizer(br.readLine());
        }
        return stk.nextToken();
    }
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
}
