#include <bits/stdc++.h>

#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)

#define endl "\n"
#define llu unsigned long long int
#define ll long long int
#define fore(i, n) for(auto i = 0; i < n ; i++)
#define forb(i, n) for(auto i = n-1; i >= 0; i++)
#define pair(k, z) pair<k, z>
#define vec(k) vector<k>
#define map(k, z) map<k, z> 
#define set(k) set<k>
#define gcd(a, b) __gcd(a,b)
#define lcm(a,b) a*b/__gcd(a,b)
#define pb push_back
#define ins insert
using namespace std;

int main()
{
    fastio;
    int t;
    cin >> t;
    
    while(t--)
    {
        int n, x, max, ele;
        cin >> n;
        vec(int) a, b;
        map<int, int> mape;
        for(int i = 0; i < n; i++)
        {
            cin >> x;
            a.pb(x);
        }
        x = 0;
        for(int i = 0; i < a.size(); i++)
        {
            max = -1;
            for(int j = 0; j < a.size(); j++)
            {
                if(mape.find(a[j]) == mape.end())
                {
                    if(x != a[j] && gcd(x, a[j]) > max)
                    {
                        max = gcd(x, a[j]);
                        ele = a[j];
                    }
                }
            }
            b.pb(ele);
            x = ele;
            mape[ele]++;
        }
        for(auto y : b)
            cout << y << " ";
        cout << "\n";
    }
    return 0;
}