#include <bits/stdc++.h>
using namespace std;
void FAST() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long gcd(long long x, long long y) {
  if (x == 0) return y;
  return gcd(y % x, x);
}
long long powM(long long x, long long y, long long m) {
  long long ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}
map<long long, long long> factorize(long long n) {
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ans[i]++;
      n /= i;
    }
  }
  if (n > 1) {
    ans[n]++;
    n = 1;
  }
  return ans;
}
int main() {
  FAST();
  long long t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<vector<int> > mult(1001);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a.begin(), a.end(), greater<int>());
    for (int d = 2; d <= 1000; d++) {
      for (int i = 0; i < n; i++) {
        if (a[i] % d == 0 && a[i] != -1) {
          mult[d].push_back(a[i]);
          a[i] = -1;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (a[i] % 1 == 0 && a[i] != -1) {
        mult[1].push_back(a[i]);
        a[i] = -1;
      }
    }
    int nn = n;
    for (nn = n; nn > 0; --nn) {
      for (int i = 1000; i >= 0; --i) {
        if (nn == mult[i].size() && nn != 0) {
          sort(mult[i].begin(), mult[i].end(), greater<int>());
          for (auto x : mult[i]) cout << x << " ";
        }
      }
    }
    cout << '\n';
  }
  return 0;
}
