#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define int long long
#define ld long double
#define fi first
#define se second
#define all(uiet) uiet.begin(),uiet.end()
#define read(UIET) for(int i = 0; i < n; ++i) cin >> UIET[i]
#define out(UIET) for(int i = 0; i < n; ++i) cout << UIET[i]
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vpp vector<pair< int, int > >
#define pll pair<int , int >
#define ppll pair < pll , pll >
#define debug(n1) cout << n1 << endl
#define len(a) ((int) (a).size())
#define endl "\n"
#define mod 1000000007
const int INF = (1LL<<60)-1;
const int maxN = 1000001;
int32_t main(){
    SPEED;
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int arr[n];
        read(arr);
        vector<int> ans;
        int Max = *max_element(arr, arr + n);
        ans.pb(Max);
        int gcd = Max;
        int cnt = 1;
        for(int i = 0; i < n; ++i){
            if(arr[i] == Max){
                arr[i] = -1;
            }
        }
        for(int i = 1; i < n; ++i){
            int temp = 0, track = 0;
            for(int j = 0; j < n; ++j){
                if(arr[j] == -1) continue;
                int cal = __gcd(gcd, arr[j]);
                if(cal > track){
                    track = cal;
                    temp = arr[j];
                }
                else if(cal == track){
                    if(arr[j] > temp){
                        temp = arr[j];
                    }
                }
            }
            for(int j = 0; j < n; ++j){
                if(arr[j] == temp){
                    arr[j] = -1;
                    break;
                }
            }
            ans.pb(temp);
            gcd = __gcd(gcd, temp);
        }
        for(int x: ans){
            cout << x << " ";
        }
        cout << endl;
    }
}