#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
  if (n <= 1) return false;
  if (n == 2 || n == 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0) return false;
  }
  return true;
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long nCr(long long n, long long r) {
  long long res(1);
  for (long long i = 0; i < r; i++) {
    res *= (n - (i));
    res /= (i + 1);
  }
  return res;
}
long long _gcd(long long a, long long b) { return b ? _gcd(b, a % b) : a; }
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  int mi = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    mi = (v[i] > v[mi] ? i : mi);
  }
  vector<int> ans;
  ans.push_back(v[mi]);
  v[mi] = 0;
  int cg = ans[0];
  for (int i = 0; i < n; i++) {
    int x = 0, y = 0;
    for (int j = 0; j < n; j++) {
      if (v[j] && _gcd(v[j], cg) > y) {
        y = _gcd(v[j], v[i]);
        x = j;
      }
    }
    ans.push_back(v[x]);
    v[x] = 0;
    cg = y;
  }
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
