//#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include <unordered_map>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define deg(x) cout<< #x"=" << (x) << endl;
#define degg(x,y) cout<< #x"="<< (x) <<","<< #y"="<< (y) <<endl;
#define deggg(x,y,z) cout<<#x"="<<(x)<<","<<#y"="<<(y)<<","<<#z"="<<(z)<<endl;
#define sca(x) scanf("%d",&x)
#define scaa(x,y) scanf("%d%d",&x,&y)
#define scaaa(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pch(x) putchar(x)
#define ptf(x) printf("%d\n",x)
#define ptff(x,y) printf("%d %d\n",x,y)
#define ptfff(x,y,z) printf("%d %d %d\n",x,y,z)
#define space(x) printf("%d ",x)
#define mes(a,x) memset(a,x,sizeof(a))
#define PI acos(-1)
#define lowbit(x) x&-x
#define ALL(X) (X).begin(), (X).end()
#define PB push_back
#define MP make_pair
#define ls rt<<1
#define rs rt<<1|1
#define ac(t) cout<<(t)<<endl
using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef priority_queue<int,vector<int>,greater<int> > xqueue;
typedef priority_queue<int> dqueue;
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> x_rb;
//typedef tree<int,null_type,greater_equal<int>,rb_tree_tag,tree_order_statistics_node_update> d_rb;

const ll INF=9e18;
const int inf=2147483647;

ll qpow(ll a,ll b,ll mod) {
    ll r=1;
    for(; b; b>>=1) {
        if(b&1)
            r=(r*a)%mod;
        a=(a*a)%mod;
    }
    return r;
}

const int N=1e3+5;
int a[N];

void work() {
    int t;
    sca(t);
    while(t--) {
        int n;
        sca(n);
        for(int i=0;i<n;i++) {
            sca(a[i]);
        }
        sort(a,a+n);
        int f1=a[n-1];
        space(f1);
        int m=n-1,gcd=f1;
        while(m) {
            int mx=0,x=-1;
            for(int j=0;j<n-1;j++) {
                if(a[j]<0) continue;
                int tx=__gcd(a[j],gcd);
                if(tx>mx) {
                    mx=tx;
                    x=j;
                }
            }
            m--;
            space(a[x]);
            a[x]=-1;
        }
        puts("");
    }
}

int main() {
#ifdef Local
    freopen("1.txt","r",stdin);
#endif // mm
    work();
    return 0;
}
