#include <bits/stdc++.h>
using namespace std;

#define ll int

void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B) 
{ cout << ' ' << A; my_dbg(B...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i,ini,n) for(ll i=ini;i<(int)n;i++)
#define all(x) x.begin(),x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define tc   int tt; cin>>tt; while(tt--)
#define gcd __gcd
#define endl "\n"
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll mod =1e9+7;
const int N = 1e3+10;

bool vis[N];
ll b[N];
 
int main()
{
    fast();

    tc
    {
        ll n;
        cin>>n;

        ll a[n+1];
        rep(i,1,n+1)
        {
            cin>>a[i];
            vis[i]=false;
        }

        ll p=0;

        rep(i,1,n+1)
        {
            if(a[i]>a[p])
            p=i;
        }

        ll curr=a[p];
        b[1]=curr;
        vis[p]=true;

        ll mcurr;

        rep(i,2,n+1)
        {
            mcurr=-1;
            rep(j,1,n+1)
            {
                if(!vis[j] && gcd(a[j],curr)>mcurr)
                mcurr=gcd(a[j],curr),p=j;
            }

            vis[p]=true;
            curr=mcurr;
            b[i]=a[p];
        }

        rep(i,1,n+1)
        cout<<b[i]<<" ";

        cout<<endl;

    }

return 0;
}













