#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vc = vector<char>;
using ull = unsigned long long;
using pi = pair<int, int>;
using vl = vector<long long>;
using pl = pair<long long, long long>;
using vpl = vector<pair<long long, long long>>;
using msl = map<string, long long>;
using vvl = vector<vector<long long>>;
const ll MOD = 1e9 + 7;
const int INF = int(1e9);
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isPrime(ll n) {
  if (n == 2) return true;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
unsigned long long factorial(int n) {
  const unsigned int M = MOD;
  unsigned long long first = 1;
  for (int i = 1; i <= n; i++) first = (first * i) % M;
  return first;
}
void solve() {
  ll n;
  cin >> n;
  vl a(n);
  int maxi = 0;
  for (int i = 0; i <= n - 1; i++) {
    cin >> a[i];
    if (a[i] > a[maxi]) {
      maxi = i;
    }
  }
  vl b(n);
  b[0] = a[maxi];
  a[maxi] = 0;
  int maxGcd = b[0];
  for (int i = 1; i <= n - 1; i++) {
    int index = 0;
    int d = 0;
    for (int j = 0; j <= n - 1; j++) {
      if (a[j] != 0 && gcd(a[j], maxGcd) > d) {
        index = j;
        d = gcd(a[j], maxGcd);
      }
    }
    b[i] = a[index];
    a[index] = 0;
    maxi = index;
  }
  for (int i = 0; i <= n - 1; i++) {
    cout << b[i] << " ";
  }
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  cin >> t;
  while (t--) solve();
  return 0;
}
