#include <bits/stdc++.h>
typedef long long ll;
#define e cout<<endl;
#define pb push_back
#define ff first
#define ss second
using namespace std;
void solve(){
    int n;
    cin>>n;
    int max2=INT_MIN;
    int index1;
    vector<int>a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(a[i]>max2){
            max2=a[i];
            index1=i;
        }
    }
    if(n==1){
        cout<<a[0];
        e
        return;
    }
    //sort(a.begin(),a.end());
    vector<int>ans;
    ans.pb(a[index1]);
    unordered_set<int>s;
    s.insert(index1);
    while(1){
        int t=ans.size();
        int x=ans[t-1];
        int max1=INT_MIN;
        int index=-1;
        for(int i=0;i<n;i++){
            if(s.count(i)>0) continue;
            int temp=__gcd(x,a[i]);
            if(temp>max1){
                max1=temp;
                index=i;
            }
        }
        ans.pb(a[index]);
        s.insert(index);
        if(ans.size()==n) break;
    }
    for(auto i:ans) cout<<i<<" ";
    e
}
int main()
{
    ll t;
    cin>>t;
    while(t--){
       solve();
    }
    return 0;
}