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

#define ll long long
#define ld long double
#define lli long long int

#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define um unordered_map
#define pb push_back

#define pll pair<ll,ll>
#define smax(a,b) a=max(a,b)
#define smin(a,b) a=min(a,b)
#define all(a) a.begin(),a.end()

#define f(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(int i=a;i>=b;i--)

// ios_base::sync_with_stdio(false);
// turn off cin.tie for interactive
// cin.tie(NULL);
// declare 2d vector 
// vector<vector<int>> v(n, vector<int> (n, 0));

int find_inverse(ll base, ll n, ll m){
    if (n == 0) return 1;
    if (n == 1) return base;

    ll half_n = find_inverse(base, n/2, m);
    ll p = (half_n * half_n) % m;
    if (n % 2 == 0) return  p;
    else return (p*base) % m;
}
int ans = 0;
int height;
// void dfs(vector<int> list[], int node, int reachedBy){
// 	f(i, 0, list[node].size()){
// 		if (list[node][i] != reachedBy){
// 			height++;
// 			ans = max(ans, height);
// 			dfs(list, list[node][i], node);
// 		}
// 	}
// }


void dfs(int &val, vector<bool> &visited, int v, int &maxi, int &mini, auto res) {
	visited[v] = true;
	for(auto it: res[v]) {
		if (visited[it]) continue;
		else {
			val++;
			dfs(val, visited, it, maxi, mini, res);
		}
	}
}

void solve(){
    int n;
    cin >> n;
    vc<int> v(n);
    int maxi;
    int maxx = 0;
    f(i, 0, n){
        cin >> v[i];
        if (v[i] > maxx){
            maxx = v[i];
            maxi = i;
        }
    }
    // cout << __gcd(100,25);
    vc<int> b(n);
    b[0] = v[maxi];
    v[maxi] = 0;
    f(i, 0, b.size()-1){
        int ogcd = 1, oposi;
        f(j, 0, v.size()){
            if (v[j] && __gcd(b[i], v[j]) >= ogcd){
                ogcd = v[j];
                oposi = j;
            }
        }
        b[i+1] = ogcd;
        v[oposi] = 0;
    }
    for (auto i: b)
        cout << i << " ";
    cout << "\n";
}


int main(){
	#ifndef ONLINE_JUDGE 
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
	int t;
	cin >> t;
	while(t--)
		solve();
}