/* Author : SHARAN SWAROOP
* Question link : 
* If you don't understand the code below...don't worry I didn't either 
*/
#include<ctime>
#include<bits/stdc++.h>
#include<iostream>
#include<string.h>
#define INF (long int)1e16
#define EPS 1e-9
#define MOD  1000000007
#define MAXN 100001
#define ll   long long 
#define endl '\n'
using namespace std;
/* ----------*/
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename T>
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; 
/*
*1. order_of_key(k) : number of elements strictly lesser than k
*2. find_by_order(k) : k-th element in the set
*To make it an ordered_multiset, use pairs of (value, time_of_insertion)
*to distinguish values which are similar.
*/
// --------pre_defined_functions--------------//
bool isSubSequence(string str1, string str2,int   m,int   n);//checks if str1 is a subsequence of str2//
int CEIL(int   a,int   b);
bool isPrime(int x);
int mod_exp(long long x, unsigned int y, int   p);//modular exopnentiation//
int gcd (int a, int b);//returns gcd
vector<int> Divisors(int n);//returns divisors of a number//
int getMSB(int n);//returns the Most Significant Bit //
void sayyes() {    cout<<"YES"<<endl;  }
void sayno()  {     cout<<"NO"<<endl;  }
/*------------------------------------------------------------*/

void solve(){
    //write code here//
    int n ; 
    cin >> n ;
    vector<int> a(n) ;
    for(auto &i : a)    cin >> i ; 
    int mx = max_element(a.begin(), a.end()) - a.begin() ; 
    vector<int> b(n) ; 
    b[0] = a[mx] ;
    a[mx] = 0 ;  
    for(int i = 1 ; i < n ; i++){
        int g = 0, x = 0 ; 
        for(int j = 0 ; j < n ; j++){
            if((a[j] != 0) &&( __gcd(b[i-1], a[j]) > g)){
                g = gcd(b[i-1], a[j]) ;
                x = j ;
            }
        }
        b[i] = a[x] ;
        a[x] = 0 ; 
    }
    for(auto i : b) cout << i << " " ;
    cout << endl ;
    //--------------//
}

int main(){
   #ifndef ONLINE_JUDGE
   freopen("INPUT.txt","r",stdin);
   freopen("OUTPUT.txt","w",stdout);
   #endif
   auto start = std::chrono::system_clock::now();

   int   t = 1 ;
   scanf("%d" , &t);/*comment this line if no test cases*/
   while(t--)
       solve();
   // -----for run-time calculation -------//
   auto end = std::chrono::system_clock::now();
   std::chrono::duration<double> elapsed_seconds = end-start;
   std::time_t end_time = std::chrono::system_clock::to_time_t(end);
   #ifndef ONLINE_JUDGE
   cout<<"RUN TIME : "<<elapsed_seconds.count()<<"s"<<endl;
   #endif
   //------//
   return 0;
}
//-----function definitions----------//
bool isSubSequence(string str1, string str2,int m,int n){/*checks if str1 is a subsequence of str2*/
   int j = 0; 
   for (int i = 0; i < n && j < m; i++){
       if (str1[j] == str2[i])
       j++;
   }
   return (j == m);
}
int CEIL(int   a,int   b){
   if(a%b==0)
   return a/b;
   else
   return (a/b)+1;
}
bool isPrime(int x){/*sqrt(n) complexity*/ 
   if (x <= 1) 
       return false; 
   for (int i = 2,sqroot = sqrt(x); i <=sqroot; i++) 
       if (x % i == 0) 
           return false; 
   return true; 
}
int mod_exp(long long x, unsigned int y, int   p){
   int res = 1;    
   x = x % p; 
   if (x == 0) return 0; 
   while (y > 0){
       if (y & 1)
           res = (res*x) % p;
       y = y>>1; 
       x = (x*x) % p;
   }
   return res;
}
vector<int> Divisors(int n){
   vector<int> div ; 
   for (int i=1; i<=sqrt(n); i++){
       if (n%i == 0){
           if (n/i == i)
               div.push_back(i);
            else{
               div.push_back(i); 
               div.push_back(n/i);
           }
       }
   }
   return div ; 
}
int gcd (int a, int b) {
   return b ? gcd (b, a % b) : a;
}
int getMSB(int n){
   if (n == 0)
       return 0;
   int msb = 0;
    n = n / 2;
    while (n != 0) {
        n = n / 2;
        msb++;
    }
return msb;
}
//--------------------------------------------------------//