#include <bits/stdc++.h>  // This will work only for g++ compiler

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,greater<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forrc(i, r, l) for (int i = (int)(r); i >= (int)(l); --i)
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define FASTIO ios::sync_with_stdio(false); cin.tie(0); //for faster IO


//short hand for usual tokens
#define pb push_back
#define fi first
#define se second


// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin(), (x).rend() //reverse traversal


// traversal function to avoid long template definition. Now with C++11 auto alleviates the pain.
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)


// find if a given value is present in a container. Container version. Runs in log(n) for set and map
#define present(c,x) ((c).find(x) != (c).end())


//find version works for all containers. This is present in std namespace
#define cpresent(c,x) (find(all(c),x) != (c).end())


// Avoiding wrap around of size()-1 where size is a unsigned int.
#define size(a) int((a).size())


// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

const ll INF = 1e18+7;
int maxLength=1;

void solve(){
    int n;
    cin>>n;
    vi arr(n),b;
    for0(i,n)cin>>arr[i];
    sort(all(arr));
    bool visited[n]={0};
    visited[size(arr)-1]=1;
    b.pb(arr[size(arr)-1]);
    int currgcd=b[0],num=0,mx=-1,g;
    while(size(b)!=n){
        for0(i,n){
            if(visited[i])continue;
            g = __gcd(currgcd,arr[i]);
            if(mx<g){
                mx=g;
                num=i;
            }
        }
        b.pb(arr[num]);
        mx=-1;
        currgcd=g;
        visited[num]=1;
    }
    for(int a:b)cout<<a<<' ';
    cout<<"\n";
}


int main() {
    FASTIO
    cout.precision(10);
    cout << fixed;
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}