#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;

        int a[n], b[n];
        vector<bool> vis(n, false);
        for(int i=0; i<n; i++) cin>>a[i];

        sort(a, a+n, greater<int>());

        b[0] = a[0];
        vis[0] = true;
        int gcd = a[0];
        for(int i=1; i<n; i++){
            int j = 1;
            while(vis[j] == true){
                j++;
            }
            int maxgcd = __gcd(a[j], gcd);
            int maxInd = j;

            for(int k = j; k<n; k++){
                if(!vis[k]){
                    if(__gcd(gcd, a[k]) > maxgcd){
                        maxgcd = __gcd(gcd, a[k]);
                        maxInd = k;
                    }
                }
            }

            b[i] = a[maxInd];
            vis[maxInd] = true;
        }

        for(int i=0; i<n; i++){
            cout<<b[i]<<" ";
        }

        cout<<endl;
    }
    return 0;
}