#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long           ll;
typedef unsigned long long  ull;
typedef long double         ld;
typedef pair<int, int>      pii;
typedef pair<ll,ll>         pll;
typedef vector<int>         vi;
typedef vector<bool>        vb;
typedef vector<pii>         vii;
typedef vector<vi >         vvi;
typedef vector<pll>         vlll;
typedef set<int>            si;
typedef map<string, int>    msi;
typedef map<int, int>       mii;
typedef map<ll, ll>         mll;
typedef map<char, int>      mci;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(v) v.begin(),v.end()
#define mem(dp,val) memset(dp,val,sizeof(dp))
#define allr(v) v.rbegin(),v.rend()
#define pb emplace_back
#define mp make_pair
#define vi vector<int>
#define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define vll vector<ll>
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define endl "\n"
#define ff first
#define ss second
#define PI 3.14159265358979323846
#define INF 1e9+5
#define INFLL 1e18+5
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define bit(x,j) ((x>>j)&1)
#define sz(a) ll((a).size())
#define PRINT(v) for(auto t:v) cout<<t<<" ";cout<<endl;
#define PRINTDP(v) for(auto t:v){for(auto u:t){ cout<<u<<" ";} cout<<endl;}
#define PRINTM(mpp) for(auto t:mpp) cout<<t.ff<<" "<<t.ss<<endl;
#define read(n) vi v(n); rep(i,0,n) cin>>v[i];
const ll MOD=1e9+7 ;
ll gcdEx(ll a, ll b, ll& x, ll& y) {x = 1, y = 0; ll x1 = 0, y1 = 1, a1 = a, b1 = b;while(b1){ ll q = a1 / b1;tie(x, x1) = make_tuple(x1, x - q * x1); tie(y, y1) = make_tuple(y1, y - q * y1);
tie(a1, b1) = make_tuple(b1, a1 - q * b1);} return a1;}
ll modI(ll a,ll m=MOD){ll x, y; ll g = gcdEx(a, m, x, y);if(g != 1) return -1; else return(x % m + m) % m;}
ll modD(ll a, ll b,ll m=MOD){return (modI(b,m)*(a%m))%m;}
ll modS(ll a, ll b,ll m=MOD){return ((a%m)-(b%m)+m)%m;}
ll modP(ll a, ll b,ll m=MOD){a %= m;ll res = 1;while (b > 0){if (b & 1) res = res * a % m; a = a * a % m; b >>= 1;}return res;}
ll modA(ll a, ll b,ll m=MOD){return ((a%m)+(b%m))%m;}
ll modM(ll a, ll b,ll m=MOD){return ((a%m)*(b%m))%m;}
ll calc(auto &v){ll sum=0;rep(i,0,v.size()){sum+=v[i];}return sum;}
ll factmod(ll n, ll p) {ll res = 1;while (n > 1) {res = (res * ((n/p) % 2 ?  p-1 : 1)) % p;for (ll i = 2; i <= n%p; ++i)res = (res * i) % p;n /= p;}
return res % p;}
long long nCrMod(ll n, ll r,ll p=MOD){if (r==0) return 1;ll fac[n+1];fac[0] = 1;for(ll i=1 ; i<=n; i++) fac[i] = (fac[i-1]*i)%p;
return (fac[n]* modI(fac[r], p) % p * modI(fac[n-r], p) % p) % p;}
ll gcd(ll a,ll b){ return b? gcd(b,a%b):a; }
vll divisors(ll n){set<ll> s;for (ll i=1; i<=sqrt(n); i++){if (n%i==0){if(n/i == i) s.insert(i); else{ s.insert(i);s.insert(n/i);}}} vll v(all(s)); return v;}
vector<ll> trial_division3( ll n) {
    vector<ll> factorization;
    for (ll d : {2, 3, 5}) {
        while (n % d == 0) {
            factorization.push_back(d);
            n /= d;
        }
    }
    static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6};
    int i = 0;
    for (long long d = 7; d * d <= n; d += increments[i++]) {
        while (n % d == 0) {
            factorization.push_back(d);
            n /= d;
        }
        if (i == 8)
            i = 0;
    }
    if (n > 1)
        factorization.push_back(n);
    return factorization;
}
vector<bool> is_prime;
void sieve(ll n){
    is_prime.resize(n+1);
    fill(all(is_prime),true);
    is_prime[0]=is_prime[1]=false;
    for(int i=2;i*i<=n;i++){
        if(is_prime[i]){
            for(int j=i*i;j<=n;j+=i)
                is_prime[j]=false;
        }
    }
}
void three_min(vi &a,vi &b,vi &c,int &ans){for (auto x : a){ auto y = lower_bound(b.begin(), b.end(), x);auto z = upper_bound(c.begin(), c.end(), x);if(y == b.end() || z == c.begin()){ continue; }
z--;int k=*y,l=*z;ans = min(ans, max({x,k,l})-min({x,k,l})+1);}} //Useful when have to choose three nearest element from three vectors

void solve(){
    int n;
    cin>>n;
    read(n);
    vi ans;
    bool done[n];
    memset(done,0,sizeof(done));
    int m=-INF;
    auto it=max_element(all(v));
    int l=it-v.begin();
    done[l]=true;
    ans.pb(v[l]);
    ll k=(ll)v[l];
    int idx=l;
    while(sz(ans)!=n){
        forn(i,n){
            if(!done[i]&&gcd(k,v[i])>m){
                m=gcd(k,v[i]);
                idx=i;
            }
        }
        ans.pb(v[idx]);
        done[idx]=true;
        m=-INF;
    }
    PRINT(ans);

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.precision(17);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;
}
