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

#define endl '\n'
#define pb push_back
#define fi(a,b) for(i=a;i<=b;i++)
#define fj(a,b) for(j=a;j<=b;j++)
#define elif else if
#define ll  long long
#define vi vector<int>
#define vl vector<long long>
#define ii pair<int,int>
#define vii vector<ii>
#define ff first
#define ss second
#define speed() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mod 1000000007
#define PI 3.14159265358979323846
#define N 1000001
#define MAX 1e10

int main()
{
    speed()
    int t=1;
    cin>>t;

    while(t--)
    {
        int n,i,j,maxi=INT_MIN,idx=-1,x=1,flag=1;
        cin>>n;

        int a[n];
        int vst[n]={};
        vector<int> ans;

        fi(0,n-1)
        {
            cin>>a[i];

            if(a[i]>maxi)   maxi=a[i], idx=i;
        }
        vst[idx]=1;
        ans.pb(a[idx]);

        i=0;
        while(i<ans.size() && i<n && flag)
        {
            x=1;
            fj(0,n-1)
            {
                if(vst[j])  continue;

                int curr = __gcd(ans[i],a[j]);
                if(curr>x)
                {
                    x = curr;
                    idx = j;
                }
            }
            if(x==1)    flag=0;
            else
            {
                vst[idx]=1;
                ans.pb(a[idx]);
            }
            i++;
        }


        for(auto e : ans)   cout<<e<<" ";
        fi(0,n-1)
        {
            if(!vst[i]) cout<<a[i]<<" ";
        }
        cout<<endl;

    }
}
