#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;

int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t,i,k;
    cin>>t;
    while(t--)
    {   
        ll n;
        cin>>n;
        vector<ll> a(n);
        for(i=0;i<n;i++)
        cin>>a[i];
        multimap<ll,ll> mp;
        ll x=*max_element(a.begin(),a.end());
        for(i=0;i<n;i++)
        {   ll z=a[i];
            
            ll c=__gcd(z,x);
            mp.insert({c,z});
            
        }
        
        for (auto i = mp.rbegin(); i != mp.rend(); ++i)
        { 
  
        cout<<i->second<<" "; 
             
        } 
        
        cout<<endl;


    }
    
}