#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() 
{
   ll t;
   cin>>t;
   while(t--)
   {
       ll n,i,x,j;
       cin>>n;
       ll cnt0=0,cnt1=0;
       vector<ll> v;
       vector<bool> vs(n,false);
       for(i=0;i<n;i++)
       {
           cin>>x;
           v.push_back(x);
       }
       vector<ll> ans;
       ll gcd=0;
       for(i=0;i<n;i++)
       {
           ll gcd1=0;
           ll gcdind=0;
           for(j=i;j<n;j++)
           {
               if(vs[j]==false&&(__gcd(v[j],gcd)>gcd1))
               {
                   gcdind=j;
                   gcd1=__gcd(v[j],gcd);
               }
           }
           ans.push_back(v[gcdind]);
           vs[gcdind]=true;
           gcd=gcd1;
       }
       for(i=0;i<n;i++)
       {
           cout<<ans[i]<<" ";
       }
       cout<<endl;
   }
}