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

int main()
{
  int t ;cin >>t ;
  while (t--){

    int n ;cin >>n ;
    vector <int >v(n);
    for (auto &x:v){

        cin >>x ;
    }
    sort (v.begin (), v.end(), greater<int >());
    for (int i =0 ; i <n-1 ; i++){
        for (int j =i+1 ; j <n ; j++){

            if (__gcd(v[i],v[i+1])<__gcd(v[i],v[j])){
                swap(v[j],v[i+1]);
            }

        }


    }

    for (auto &x:v ){

        cout <<x <<" ";
    }cout <<endl;
  }
}
