#include <bits/stdc++.h>
#define int long long 
#define ff first
#define ss second
using namespace std;


void solve()
{
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    reverse(a,a+n);
    vector<int>v;
    v.push_back(a[0]) ;
    
    while(v.size() != n)
    {
        //if(a[i] == -1 ) continue;
        priority_queue<pair<int,int>>pq;
        int curr = v.back();
        for(int j=1;j<n;j++)
        {
            if(a[j] == -1)  continue;
            
            int gcd = __gcd(curr , a[j]);
            pq.push({gcd , j}) ;
        }
        auto it = pq.top();
        curr = a[it.ss];
        //cout << "ele=" << a[it.ss] << '\n';
        v.push_back(a[it.ss]);
        a[it.ss] = -1;
    }
    
    for(auto &it : v)
        cout << it << " " ;
    cout << '\n';
}


signed main() 
{
	int t;
	cin>>t;
	while(t--)
	{
	    solve();
	}
	return 0;
}