#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool cond(pair<int,int>a,pair<int,int>b){
    if(a.first<b.first){
      return true;
    }
    else if(a.first==b.first){
          if(a.second<b.second){
            return true;
        }
        return false;
    }
    return false;
}


int main(){
   int t; cin>>t; 
   while(t--){
       int n; cin>>n;
      vector <int> a;
      for(int i=0;i<n;i++){
          int temp; cin>>temp;
          a.push_back(temp);
      }
      bool visited[n]; 
      int g =1; 
      memset(visited,false,sizeof(visited)); vector <int> ans;
      for(int i=0;i<n;i++){
          vector <pair<int,int>> v;
          for(int j=0;j<n;j++){
              if(!visited[j]){
              v.push_back({__gcd(a[j],g),j});
              }
          }
          sort(v.begin(),v.end(),cond);
          if(i==0){
              g = a[v[v.size()-1].second];
          }
          else{
              g = __gcd(g,a[v[v.size()-1].second]);
          }

            ans.push_back(a[v[v.size()-1].second]);
            visited[v[v.size()-1].second]=true;
      }

for(auto x:ans){
    cout<<x<<" ";
}cout<<endl;
      
       
   }
}

