#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define sll set<ll>
#define vpll vector<pair<ll,ll>>
#define stkll stack<ll> 
#define quell queue<ll>
#define mpll map<ll,ll>  
#define pb push_back
#define fi first
#define se second
#define sz size()
#define be begin()
#define en end()
#define looop for(ll i=0;i<n;i++)
#define loop(i,x) for(ll i=0;i<x;i++)
#define lop(i,a,x) for(ll i=a;i<x;i++)
#define PI 3.1415926535
ll mod=1e9 + 7;
ll max(int a,ll b){ return max((ll)a,b);}
ll min(int a,ll b){ return min((ll)a,b);}
ll min(ll a,int b){ return min(a,(ll)b);}
ll max(ll a,int b){ return max(a,(ll)b);}
ll power(ll a,ll b){
    ll ans=1;
    while(b>0){
        if(b&1){ans=(ans*a)%mod;}
                a=(a*a)%mod;
                b>>=1;
        }
    return ans;}
ll modu(ll k)
{if(k>=0)
return k;
else
return -k;
}
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        vll arr(n);
        looop
        cin>>arr[i];
        sort(arr.be,arr.en,greater<ll> ());
        vll brr;
        brr.pb(arr[0]);
        vector<bool> mark(n,false);
        mark[0]=true;
        ll p=0;
        ll maxgcd=0;
        ll curr=arr[0];
        for(ll i=0;i<n;i++)
        {  maxgcd=0;
            
            for(ll j=0;j<n;j++)
            {   if(mark[j]==false)
              {
                ll k=__gcd(curr,arr[j]);
                if(k>maxgcd)
                {  //cout<<"yyy"<<endl;
                    p=j;
                    maxgcd=k;
                }
              }
            }
            
            if(maxgcd==0)
            break;
            mark[p]=true;
           // cout<<p<<arr[p]<<endl;
            brr.pb(arr[p]);
            
            curr=arr[p];
            if(maxgcd==1)
            break;
        }
        looop
        {
            if(mark[i]==false)
            brr.pb(arr[i]);
        }
        looop
        cout<<brr[i]<<" ";
        cout<<endl;
    }
}