import java.io.*;
import java.util.*;
import java.math.*;

public class B669 {
	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 void main(String[] args) {
		FastReader scan = new FastReader();
		int t = scan.nextInt();
		for (int i = 0; i < t; i++) {
			testCase(scan);
		}
	}
		
	static void testCase(FastReader scan) {
		int n = scan.nextInt();
		HashSet<Integer> set = new HashSet<Integer>();
		int max = 0;
		for (int i = 0; i < n; i++) {
			int next = scan.nextInt();
			set.add(next);
			max = Math.max(max, next);
		}
		int gcd [] = new int [n];
		int out [] = new int [n];
		gcd[0] = max;
		out[0] = max;
		set.remove((Integer) max);
		max = 0;
		int maxgcd = 0;
		for (int i = 0; i < n - 1; i++) {
			for(Integer in : set) {
				int result = gcd(gcd[i], in);
				if( result > maxgcd) {
					maxgcd = result;
					max = in;
				}
			}
			
			set.remove((Integer) max);
			out[i+1] = max;
			gcd[i+1] = maxgcd;
			max = 0;
			maxgcd = 0;
		}
			
		for (int i = 0; i < out.length; i++) {
			System.out.print(out[i] + " ");
			
		}
		System.out.println();
	}
	
	public static int gcd(int a, int b) {
		if (b==0) return a;
		return gcd(b, a%b);
	}

}
