
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
  
  int t;
  cin>>t;
  while(t--)
  {
      
      int n,max1=0,indx=-1;
      cin>>n;
      int arr[n+1];
      for(int i=0;i<n;i++)
      {
          cin>>arr[i];
          if(arr[i]>max1)
          {
           max1=max(max1,arr[i]);
           indx=i;
          }
          
      }
        vector<int>cnt(1002,0);
      
      int mal=0,sec=-1;
      for(int val=0;val<n;val++)
      {
            cnt[max1]=1;
            cout<<max1<<" ";
          for(int j=0;j<n;j++)
          {
              if(indx!=j&&cnt[arr[j]]==0)
              {
                  if(__gcd(max1,arr[j])>mal)
                  {
                      mal=__gcd(max1,arr[j]);
                      
                      sec=arr[j];
                      indx=j;
                  }
              }
          }
          max1=sec;
          sec=-1;
          mal=0;
      }
      
      cout<<endl;
  }
  
  
  return 0;
}
