#include<bits/stdc++.h> 
using namespace std; 
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define mod 998244353
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)  
{  
    ll 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;  
}
bool sortinrev(const pair<vector<int>,int>&a,const pair<vector<int>,int>&b){if(a.fi==b.fi)return b.se>a.se;return a.fi>b.fi;}
void run(vector<int>v){for(int i=0;i<v.size();i++)cout<<v[i]<<" ";cout<<endl;}
int modInverse(ll a, ll m) 
{ ll m0 = m;   ll y = 0, x = 1; if (m == 1) return 0; while (a > 1) { 
ll q = a / m;ll t = m; m = a % m, a = t; t = y;y = x - q * y; x = t; } if (x < 0) x += m0; return x; } 
void solve()
{
    int n;
    cin>>n;
    int a[n];vector<int>ans(n,0),finale;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int maxi = *max_element(a,a+n);
    for(int i=0;i<n;i++)
    {
        if(a[i]==maxi)
        {
            ans[i]=1;
            finale.pb(maxi);
        }
    }
    int curr=maxi;
    for(int i=0;i<n;i++)
    {
        int gcd1=0,ind=-1;
        for(int j=0;j<n;j++)
        {
            if(ans[j]==0)
            {
                if(__gcd(curr,a[j])>gcd1)
                {
                    gcd1 = __gcd(curr,a[j]);
                    ind=j;
                }
            }
        }
        if(ind!=-1)
        {
            ans[ind]=1;
            finale.pb(a[ind]);
        }
        
    }
    run(finale);



}   

int main() 
{ 
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output4.txt", "w", stdout);
    #endif
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0; 
} 