#include <bits/stdc++.h>

#define ll long long int
#define endl "\n"
#define mod 1000000007

using namespace std;

void runTest()
{
    int n;
    cin >> n;

    set<ll> a;
    map<ll, int> m;
    for (int i = 0; i < n; i++)
    {
        ll val;
        cin >> val;

        m[val]++;
        a.insert(val);
    }

    // sort(a.begin(), a.end(), greater<ll>());

    vector<ll> ans;
    while (m[*prev(a.end())] > 0)
    {
        ans.push_back(*prev(a.end()));
        m[*prev(a.end())]--;
    }

    ll gc = *prev(a.end());
    a.erase(prev(a.end()));
    while (a.size() > 0)
    {
        ll maxi = INT_MIN;
        ll val = -1;
        for (auto it : a)
        {
            ll temp = __gcd(gc, it);
            if (temp > maxi)
            {
                maxi = temp;
                val = it;
            }
        }
        while (m[val] > 0)
        {
            ans.push_back(val);
            m[val]--;
        }
        a.erase(val);
    }

    for (auto it : ans)
        cout << it << ' ';
    cout << endl;
}

int main()
{

#if !defined(ONLINE_JUDGE)
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    cin >> t;

    while (t--)
        runTest();

    return 0;
}