/*****************************************************///
/*.......................Faiyaj Bin Amin............................*///
/*.........................KUET CSE 2K17......................*///
/*****************************************************///

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define fastio      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define forn(i, n) for(int i=0; i<n; i++)
#define ll long long
#define pb push_back
#define em emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
#define pii pair<int,int>

#define see(x)        cerr<< __LINE__ << ": " << (#x) << " is "<< (x) <<'\n';
#define seecontent(v) cout << (#v) << " is "; for(auto x : v) { cout << x << " ";} cout << "\n\n";

#define PI 2*acos(0.0)
#define lcm(a, b)  ((a / __gcd(a, b) ) * b)

const int MOD    = 1e9+7;
const int mx     = 1e2+5;
#define INF 1e18
#define MIN 0

#define filein freopen ("input.txt", "r", stdin)
#define fileout freopen ("output.txt", "w", stdout)

//template <typename T>  using orderedSet =
//    tree<T, null_type, less_equal<T>,
//    rb_tree_tag, tree_order_statistics_node_update>; //find_by_order, order_of_key
//
//
//------------------------------Graph Moves----------------------------
//const int fx[]={+1,-1,+0,+0}; // Directional
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
//---------------------------------------------------------------------
//
//ll powmod(ll a,ll b){ll res=1;a%=mod;assert(b>=0);for(; b; b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//
//*------------------------------------BITMASK------------------------------------------*
//inline int Change(int N, int pos){return N= N^(1<<pos);}
//inline bool Check(int N, int pos){return (bool)(N &(1<<pos));}  /// check bit 0 or 1
//inline int setBit(int N,int pos){return N=N | (1<<pos);}
//inline int resetBit(int N,int pos){return N= N & ~(1<<pos);}
//*-----------------------------------------------------------------------------------------------*
//
//using namespace __gnu_pbds;
using namespace std;


int main()
{
    fastio;
    int t;
    cin>>t;

    while(t--)
    {
        int n;
        cin>>n;

        int a[n], maxi=-1, pmax=-1;
        vector<int> ans;
        forn(i, n)
        {
            cin>>a[i];
            if(a[i]>maxi)
            {
                maxi=a[i];
                pmax=i;
            }
        }
        ans.em(maxi);
        a[pmax]=0;

        forn(i, n)
        {
            int gm=-1;
            forn(j, n)
            {
                if(a[j]!=0)
                {
                    int g=__gcd(ans.back(), a[j]);
                    if(g>gm)
                    {
                        gm=g;
                        pmax=j;
                    }
                }
            }
            if(gm!=-1)
            {
                ans.em(a[pmax]);
                a[pmax]=0;
            }

            if(ans.size()==n)
                break;
        }

        forn(i, ans.size())
        {
            cout << ans[i] << " ";
        }
        cout << "\n";
    }

}



