#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

inline void solve() {
    int n; cin >> n;
    int MAX = 0;
    vector<pii> vec(n);
    for(int i = 0; i < n; ++i)
        cin >> vec[i].F, MAX = max(MAX, vec[i].F);
    for(int i = 0; i < n; ++i) {
        vec[i].S = __gcd(vec[i].F, MAX);
    }
    sort(vec.begin(), vec.end(), [](pii a, pii b) {
        return a.S > b.S;
    });
    for(auto& x : vec)
        cout << x.F << " ";
    cout << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
    cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("err.txt", "w", stderr);
    clock_t time = clock();
#endif
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }

#ifdef IOFILE
    cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}