



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

public class Main 

{ 

	 static FastReader sc=new FastReader(); 
	 
		  public static void main(String[] args) 
    {
			  StringBuffer sb=new StringBuffer("");
				int t = i(); 
		        while (t-- > 0) 
				{ 
		        	int n=i();
		        	
		        	int A[]=input(n);
		        	Integer B[]=new Integer[n];
		        	for(int i=0;i<n;i++)
		        		B[i]=A[i];
		        	
		        	
		        	Arrays.sort(B,Collections.reverseOrder());
		        	for(int i=0;i<n;i++)
		        		A[i]=B[i];
		        	
		        	int gcd=A[0];
		        	int max=0;
		        	ArrayList<Integer> l=new ArrayList<Integer>();
		        	HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
		        	map.put(0, 1);
		        	l.add(A[0]);
		        	
		        	int ind=0;
		        	while(l.size()!=n) {
		        		max=0;
		        		ind=-1;
		        	for(int i=1;i<n;i++) {
		        		if(map.containsKey(i))
		        			continue;
		        		int y=gcd(gcd,A[i]);
		        		if(y>=max) {
		        			max=y;
		        			ind=i;
		        		}
		        	}
		        	l.add(A[ind]);
		        	map.put(ind,1);
		        	
				}
		        	for(int i : l) {
		        		System.out.print(i+" ");
		        	}
		        	System.out.println();
		        	
				}
		        //System.out.println(sb.toString());
		        	
				
    }
				
		  static int gcd(int a, int b) 
		    { 
		      if (b == 0) 
		        return a; 
		      return gcd(b, a % b);  
		    } 

static int[] input(int n) {
	int A[]=new int[n];
	   for(int i=0;i<n;i++) {
		   A[i]=sc.nextInt();
	   }
	   return A;
   }
static long[] inputL(int n) {
	long A[]=new long[n];
	   for(int i=0;i<n;i++) {
		   A[i]=sc.nextLong();
	   }
	   return A;
   }
static String[] inputS(int n) {
	String A[]=new String[n];
	   for(int i=0;i<n;i++) {
		   A[i]=sc.next();
	   }
	   return A;
   }


static void input(int A[],int B[]) {
	   for(int i=0;i<A.length;i++) {
		   A[i]=sc.nextInt();
		   B[i]=sc.nextInt();
	   }
}
     static int i() {
    	 return sc.nextInt();
     }
     static String s() {
    	 return sc.next();
     }
     static long l() {
    	 return sc.nextLong();
     }  
     static void sort(int[] arr){
        int n = arr.length;
        Random rnd = new Random();
        for(int i=0; i<n; ++i){
            int tmp = arr[i];
            int randomPos = i + rnd.nextInt(n-i);
            arr[i] = arr[randomPos];
            arr[randomPos] = tmp;
        }
        Arrays.sort(arr);
     }
  
        
        
    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; 
        } 
    } 
} 



