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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pdd pair<double,double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt","r",stdin);
#define write freopen("vorta.txt","w",stdout);
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
#define PI 2*acos(0.0)
#define eps 1e-11
#define DEBUG(x) cerr << #x << " = " << x << endl
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

const int MAX = 2e5 + 5, MOD = 1e9 + 7  , MAXLG = log2(MAX)+1;
const ll inf = 1e18 + 5;

int arr[MAX], vis[MAX];
int main(){

    fastio;
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int mx = -1, mx_id = -1;
        for(int i=0; i<n; i++){
            cin >> arr[i];
            vis[i] = false;
            if(mx < arr[i]){
                mx = arr[i];
                mx_id = i;
            }
        }

        vis[mx_id] = true;
        int gcd = mx;
        cout << gcd << " ";

        for(int i=1; i<n; i++){
            mx = -1, mx_id = -1;
            for(int j=0; j<n; j++){
                if(vis[j]) continue;
                int gcd_here = __gcd(gcd, arr[j]);
                if(mx < gcd_here){
                    mx = gcd_here;
                    mx_id = j;
                }
            }
            vis[mx_id] = true;
            cout << arr[mx_id] << " ";
        }
        cout << "\n";
    }
}
