#include<iostream>
using namespace std;
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
int main()
{
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        int pos[2000]= {0};
        int arr[n];

        for(int i=0; i<n; i++) {
            cin>>arr[i];
            pos[arr[i]]++;

        }
        sort(arr,arr+n);
        reverse(arr,arr+n);
        int i=0,g=arr[0];

        /* while(pos[arr[0]]>0) {
             cout<<arr[0]<<" ";
             pos[arr[0]]--;
             i++;
         }
         i--;*/
        while(i<n ) {
            if(pos[arr[i]]>0) {

                int cur=arr[i];

                while(pos[cur]>0) {
                    cout<<arr[i]<<" ";
                    pos[cur]--;
                    i++;
                }
                i--;
            }
            int gd=1,mx=0,ans=0;

            for(int j=i+1; j<n; j++) {
                if(pos[arr[j]]>0) {
                    gd=__gcd(g,arr[j]);
                    if(gd>mx) {
                        ans=arr[j];
                        mx=gd;
                    }
                }
            }
            g=mx;

            while(pos[ans]>0) {
                cout<<ans<<" ";
                pos[ans]--;
            }
            i++;


        }

        cout<<endl;

    }
    return 0;
}