#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
 int n;
 cin>>n;
 int a[n];
 int taken[n]={0},gcd_so_far=0;
 vector<int>v;

 for(int i=0;i<n;i++) cin>>a[i];
 
 for(int i=0;i<n;i++)
 {
   //for subarray sizeof i+1; 
   if(i==0)
   {
    gcd_so_far=*max_element(a,a+n);
    int in=max_element(a,a+n)-a;
    v.push_back(a[in]);
    taken[in]=1;
    continue;
   }
   int maxi=0,in;
   for(int j=0;j<n;j++)
   {
      if(taken[j]>0 || i==j) continue;
      if(maxi<__gcd(gcd_so_far,a[j]))
      {
        maxi=__gcd(gcd_so_far,a[j]);
        in=j;
      }
   }
   v.push_back(a[in]);
   taken[in]=1;
   gcd_so_far=maxi;
 }
 for(auto i:v)
 {
   cout<<i<<" ";
 }
 cout<<endl;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll q;
  cin >> q;
 
  while (q--)
  {
    solve();
  }
  return 0;
}