import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import java.*;
public class Main{
    
static int gcd(int a, int b) 
    { 
      if (b == 0) 
        return a; 
      return gcd(b, a % b);  
    } 
static class Pair{
    int value;
    int gcd;
    Pair(int value,int gcd){
        this.value = value;
        this.gcd = gcd;
    }
}
static class CustomSort implements Comparator<Pair>{
    public int compare(Pair p1, Pair p2){
        return p2.gcd-p1.gcd;
    }
}
    
    public static void main(String[] args) throws Exception{
       BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bf.readLine());
        while(t-->0){
            int n = Integer.parseInt(bf.readLine());
            String[] xx = bf.readLine().split("\\s");
            int[] a = new int[n];
            // int[] b = new int[n];
            Pair[] a1 = new Pair[n];
            int max=Integer.MIN_VALUE;
            for(int i=0;i<n;i++){
                a[i]=Integer.parseInt(xx[i]);
                if(max<a[i])
                    max=a[i];
            }
         
           if(n==1){
               System.out.println(a[0]);
           }
           else{
             for(int i=0;i<n;i++){
                a1[i] = new Pair(a[i],gcd(max,a[i]));
            }
           Arrays.sort(a1,new CustomSort());
           if(a1[0].value<=a[0]){
                    int temp = a1[0].value;
                    a1[0].value = a1[1].value;
                    a1[1].value = temp;
            }
            for(int i=0;i<n;i++){
                
                System.out.print(a1[i].value + " ");
            }
            
          System.out.println();
            }
        }
    }
        }