#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define f first
#define s second
#define st(x) sort(x.begin(),x.end())
const ll N = 1e7 + 5;
const ll mod = 1e9 + 7; 
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
void solve()
{    
	ll n , m(INT_MIN) , p = 0 , x(INT_MIN) , y;
	cin >> n;
	ll a[n];
	vector<ll>ar;
	for(ll i = 0 ; i < n ; i++)
	{
		cin >> a[i];
		if(x < a[i])
		{
			x = a[i];
			y = i;
		}
	}
	ar.pb(x);
	ll r = a[0];
	a[0] = a[y];
	a[y] = r;
	for(ll i = 1 ; i < n ; i++)
	{
		for(ll j = i ; j < n ; j++)
		{
			if(m < __gcd(a[i-1],a[j]))
			{
				p = j;
				m = __gcd(a[i-1],a[j]);
			}
		}
		ar.pb(a[p]);
		r = a[i];
		a[i] = a[p];
		a[p] = r;
		a[i] = __gcd(a[i-1],a[p]);
		m = 0;
	}
	for(auto it : ar)
		cout << it << " ";
	cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t=1;
    cin>>t;
    while(t--)
    {
      solve();
    }
    return 0;  
}