

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


// @author : Dinosparton 

public class test {
	 
	   static class Pair{ 
		   int x;
		   int y;
		   
		   Pair(int x,int y){ 
			   this.x = x;
			   this.y = y;
			   
		   }
	   }
	  
	   static class Compare { 
		   
		     void compare(Pair arr[], int n) 
		    { 
		        // Comparator to sort the pair according to second element 
		        Arrays.sort(arr, new Comparator<Pair>() { 
		            @Override public int compare(Pair p1, Pair p2) 
		            { 
		                return p1.x - p2.x; 
		            } 
		        }); 
		  
//		        for (int i = 0; i < n; i++) { 
//		            System.out.print(arr[i].x + " " + arr[i].y + " "); 
//		        } 
//		        System.out.println(); 
		    } 
		} 
	  
	   static int gcd(int a, int b)
	    {
	      if (b == 0)
	        return a;
	      return gcd(b, a % b);
	    }
	 
	   public static void main(String args[]) { 
		
		  Scanner sc = new Scanner(System.in);
		  
		  int tc = sc.nextInt();
		  
		  while(tc-->0) { 
			  
			  int n = sc.nextInt();
			  
			  int a[] = new int[n];
			  
			  int maxn = 0;
			  int maxi = -1;
			  
			  for(int i=0;i<n;i++) {
				  a[i] = sc.nextInt();
				  
				  if(a[i]>maxn) { 
					  maxn = a[i];
					  maxi = i;
				  }
			  }
			  
			  boolean visit[] = new boolean[n];
			  
			 
			  
			  int output[] = new int[n];
			  
			  output[0] = a[maxi];
			  visit[maxi] = true;
			  
			  
			  for(int i=1;i<n;i++) { 
				  int max = 0;
				  int index = -1;
				  int ele = 0;
				  for(int j=0;j<n;j++) { 
					 
					  if(visit[j]==false && gcd(output[i-1],a[j])>=max && a[j]>ele) { 
						  max = gcd(output[i-1],a[j]);
						  index = j;
						  ele = a[j];
					  }
					  
				  }
				  output[i] = a[index];
				  visit[index] = true;
			  }
			  
			  for(int i=0;i<n;i++)
				  System.out.print(output[i]+" ");
			  
			  System.out.println();
		  }
		
	   }  
}  




