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

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        int mx=INT_MIN;
        int ind;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            if(a[i]>mx)
            {
                mx=a[i];
                ind=i;
            }
        }
        vector<int>v;
        v.push_back(a[ind]);
        mx=a[ind];
        a[ind]=-1;
       // sort(a,a+n,greater<int>());
        while(1)
        {
            if(v.size()==n)
            {
                break;
            }
        int mx1=INT_MIN;
        for(int i=0;i<n;i++)
        {
            if(a[i]!=-1)
            {
                if(__gcd(mx,a[i])>mx1){
                mx1=__gcd(mx,a[i]);
                ind=i;
                }
            }
        }
        if(mx1==1)
        {
            for(int i=0;i<n;i++)
            {
                if(a[i]!=-1)
                {
                    v.push_back(a[i]);
                }
            }
            break;
        }else{
        v.push_back(a[ind]);
        mx=a[ind];
        a[ind]=-1;
        }
    }
    for(int i=0;i<v.size();i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<endl;
    }
	return 0;
}
