import java.util.*;
 
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while(t-->0){
            int n = in.nextInt();
             int a[] = new int[n];
             for(int i=0;i<n;i++)
                 a[i]=in.nextInt();
             Arrays.sort(a);
             boolean flag[] = new boolean[n];
 
              int pregcd=0;
              int preidx=n;
              for(int i=n-1;i>=0;i--){
                  int maxgcd=1;
                  for(int j=n-1;j>=0;j--){
                      if(flag[j])
                          continue;
                      int gcd=Maths.gcd(pregcd,a[j]);
                      if(gcd>=maxgcd){
                          maxgcd=gcd;
                          preidx=j;
                      }
                  }
                  flag[preidx]=true;
                  pregcd=maxgcd;
                  System.out.print(a[preidx]+" ");
              }
              System.out.println();
        }
    }
    static class Maths{
          static int gcd(int a, int b) {
              if (a == 0)
                  return b;
              return gcd(b % a, a);
          }
 
 
 
          public static long factorial(int n){
              long fact = 1;
              for(int i=1;i<=n;i++){
                  fact *= i;
              }
              return fact;
          }
      }
}
