#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
bool cmp(pair<int,int> a,pair<int,int> b)
{
    if(a.first==b.first)
        return a.second>b.second;
    return a.first>b.first;
}
int main(void)
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n],m=0;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
            m=max(m,arr[i]);
        }
        vector<pair<int,int>> tp;
        int temp=m;
        for(int i=0;i<n;i++)
        {
            //if(__gcd(m,arr[i])!=1)
            {
                temp=max(temp,__gcd(temp,arr[i]));
                tp.push_back({__gcd(arr[i],temp),arr[i]});
                arr[i]=(-1);
            }
        }
        sort(tp.begin(),tp.end(),cmp);
        for(int i=0;i<tp.size();i++)
            cout<<tp[i].second<<" ";
        cout<<"\n";
    }
    return 0;
}