#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
   int t;
   cin>>t;
   while(t--){
    int n;
    cin>>n;
    vector<ll>v(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
  auto ab=max_element(v.begin(),v.end());
vector<ll>res;
res.push_back(*ab);
ll curr=*ab;
*ab=-1;

    while(res.size()<n){
            int pos=-1;
            int val=0;
        for(int i=0;i<n;i++){
            if(v[i]==-1)    continue;
            int g=__gcd(res[res.size()-1],v[i]);
            if(g>val){
                val=g;
                pos=i;
            }


        }res.push_back(v[pos]);
            v[pos]=-1;
    }
    for(int i=0;i<n;i++)    cout<<res[i]<<" ";
    cout<<endl;

   }


}
