#include <bits/stdc++.h>
#define ll long long
#define pi acos(-1.00)
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;


int main()
{

    fastio;

    ll i,j,n,m,c=0,k,t,ans,b,d,x,y,z;



    cin>>t;

    while(t--)
    {
        cin>>n;
        ll a[n];
        for(i=0;i<n;i++) cin>>a[i];

        vector<ll>v;
        v.push_back(*max_element(a,a+n));

        sort(a,a+n,greater<>());
        a[0]=-1;

        for(i=1;i<n;i++){
            x=v[v.size()-1];
            y=0;
            z=0;
            for(j=1;j<n;j++){
                if(__gcd(x,a[j])>y && a[j]!=-1){
                    y=__gcd(x,a[j]);
                    z=j;
                }
            }
            v.push_back(a[z]);
            a[z]=-1;
        }
        for(auto tt:v) cout << tt << " ";
        cout << "\n";
    }

    return 0;
}

