#include<bits/stdc++.h>
#define ll long long int
#define mii map<int,int>
#define qi queue<int>
#define pqi priority_queue<int>
#define pqgi priority_queue<int,vector<int>,greater<int>>
#define si set<int>
#define vi vector<int>
#define vll vector<long long int>
#define vii vector<pair<int,int>>
#define mii map<int,int>
#define ii pair<int,int>
#define SYNC ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define f(i,a,b) for(int i=a;i<b;i++)
#define rf(j,c,d) for(int j=c;j>d;j--)
#define mod 1000000007
#define mo 998244353
#define inf 1e18
#define F first
#define S second
#define ld long double
#define INF (int)INT_MAX
#define PI 3.1415926535898
using namespace std;
const int N=1e5+5;
clock_t timeStart, timeFinish;
void timeBegin(){
	timeStart = clock();
}
void timeEnd() {
    timeFinish = clock();
}
void timeDuration() {
    double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
    cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
    cout << " sec " << endl;
}
/*ll po(ll A,ll B)
{
	ll RES=1;
	while(B)
	{
		if(B&1)
			RES*=A;
		B>>=1;
		A*=A;
	}
	return RES;
}*/
/*void sieve(int P)
{
	prime[0]=0;prime[1]=0;
	f(i,2,P+1)
		prime[i]=1;
	f(i,2,P+1)
	{
		if(prime[i]==1)
		{
			for(int j=i*i;j<P+1;j+=i)
			{
				prime[j]=i;
			}
		}
	}
}*/
void solve()
{
	int n;
	cin>>n;
	vi v;int x;
	f(i,1,n+1)
	{
		cin>>x;
		v.push_back(x);
	}
	sort(v.begin(),v.end());
	auto it=v.end();
	it--;
	cout<<*it<<" ";
	v.erase(it);x=*it;
	while(1)
	{
		int mn=-1;
		if(v.empty())
			break;
		auto jt=v.begin();
		for(it=v.begin();it!=v.end();it++)
		{
			if(__gcd(x,*it)>mn)
			{
				mn=__gcd(x,*it);
				jt=it;
			}
		}
		x=*jt;
		cout<<*jt<<" ";
		v.erase(jt);
	}
	cout<<'\n';
	return;
}
int main()
{
    SYNC
    //sieve();
    int t=1;
    cin>>t;
    while(t--)
    	solve();
    //timeDuration();
    return 0;
}