#include <bits/stdc++.h>

#define ll long long
using namespace std;
//vector<vector<int>> dp(100, vector<int>(100, 0));
// int M = 1000000007;
// vector<vector<int>> NCR(1001, vector<int>(1001, -1));

void solve()
{
    int n;
    cin >> n;
    int a[n]{};
    for(int &i:a)
        cin >> i;
    sort(a, a + n);
    vector <bool> visited(n,0);
    vector<int> ans;
    ans.push_back(a[0]);
    visited[0] = 1;
    int p = a[0];
    while(ans.size()<n)
    {

        int tmp{},old{},in{};
        for (int i = 0; i < n ;i++)
        {
            if(visited[i])
                continue;
            tmp = __gcd(p, a[i]);
            if(tmp > old)
            {
                old = tmp;
                in = i;
            }
        }
        ans.push_back(a[in]);
        visited[in] = 1;
    }
    for (auto i : ans)
        cout << i << " ";
    cout << endl;
}
int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        // cout << endl;
    }
}
