#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define f first
#define s second
#define pb push_back
#define print(x) do { cerr << #x << ": " << x << endl; } while (0)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
const int MN = 1e3+5, LOG = 18 ,MOD = 1e9+7;
ll fpow(ll b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}
int n;
int t;
int used[MN];
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> t;
    while (t--) {
        cin >> n;
        vi a;
        vi b;
        set<int> facts;
        vi fact;
        for (int i = 0, z;i<n;i++) {
            cin >> z;a.pb(z);
            used[i] = 0;
        }
        sort(a.begin(), a.end(), greater<int>());
        for (int i = 0; i<n;i++) {
            for (int j = i+1;j<n;j++) {
                facts.insert(__gcd(a[i], a[j]));
            }
        }
        for (int i : facts){
            fact.pb(i);
        }
        sort(fact.begin(), fact.end(), greater<int>());
        for (int x : fact) {
            for (int j = 0; j<n;j++) {
                if (used[j]) {
                    continue;
                }
                if (a[j] %  x == 0) {
                    b.pb(a[j]);
                    used[j] = 1;
                }
            }
        }
        for (int i : b) {
            cout << i << " ";
        }
        cout << '\n';
    }


}