#include<bits/stdc++.h>
using namespace std;
#define lld long long int
#define speed ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
#define M 1000000007
int main()
{
    speed
    lld t;
    cin>>t;
    while(t--)
    {
        lld n;
        cin>>n;
        lld a[n+1];
        lld i,j;
        vector<lld>v;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            v.push_back(a[i]);
        }
        //sort(a,a+n,greater<lld>());
        sort(v.begin(),v.end(),greater<lld>());
        lld k=0;
        cout<<v[0]<<" ";
        lld zz=v[0];
        v.erase(v.begin()+k);
       lld  m;
       while(v.size()!=0)
       {
           m=-1;
        for(i=0;i<v.size();i++)
        {
           if(__gcd(zz,v[i])>m)
           {
              m=__gcd(zz,v[i]);
              k=i;
           }
        }
        cout<<v[k]<<" ";
        v.erase(v.begin()+k);
       }
        cout<<"\n";
    }
    return 0;
}