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

using ll  = long long;
using pii = pair<int,int>;
#define dbg(n) cout<<'\n'<<#n<<" is "<<n<<'\n';
#define pb push_back
#define all(v) v.begin(), v.end()
#define cs(i) cout<<"Case "<<i+1<<": \n"; 
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"

const int N   = 2e5+5;
const int INF = 0x7fffffff;

void solve(){
    int n;
    cin>>n;
    int a[N];
    int mx=0,ind;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }

    for(int i=0;i<n;i++){
        if(mx<a[i]) mx=max(mx,a[i]) , ind = i;
    }

    a[ind]=-1;

    vector<int>ans;
    ans.pb(mx);
    mx=0;
    int val ,prev=ans[0];
    while(ans.size()!=n){

        for(int i=0;i<n;i++){
            if(a[i]!=-1) 
                if(__gcd(prev,a[i])>mx){ mx=__gcd(prev,a[i]); ind=i; val=a[i]; }
        }
        prev=val;
        ans.pb(val);
        a[ind]=-1;
        mx=0;
    }
    //cout<<ans.size()<<endl;
    for(int x: ans) cout<<x<<" ";
    cout<<endl;
}

int main(){
    int n,t,hold=0; 
    FAST
    
    cin>>t;
    while(t--)
        solve();
    
    return 0;
}