#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define pause; system("pause");
#define mp make_pair
const int N=1e5+10;
const double eps=1e-9;
const int inf=1e9;
int a[N];
bool vis[N];
main(){

    int t;
    scanf("%d",&t);
    while(t--){

        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",a+i);

        }
        memset(vis,0,sizeof vis);
        sort(a+1,a+1+n);
        vector<int>ans;
        for(int i=n;i>0;i--){
            if(vis[i])continue;
            vector<pair<int,int> >tmp;
            tmp.bp({a[i],a[i]});
            for(int j=i-1;j>0;j--){
                if(vis[j])continue;
                int g=__gcd(a[i],a[j]);
                if(g>1){
                    tmp.bp({g,a[j]});
                    vis[j]=1;
                }
            }
            sort(tmp.begin(),tmp.end());
            int sz=tmp.size()-1;
            for(int j=sz;j>-1;j--)ans.bp(tmp[j].s);
        }

        for(auto x:ans)printf("%d ",x);
        
        printf("\n");

    }
}
