#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int power(long long int x, long long int y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
long long int modpower(long long int x, long long int n, long long int M) {
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
long long int mmi(long long int n, long long int M) {
  return modpower(n, M - 2, M);
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int a[n];
    long long int mi = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
      mi = (a[i] > a[mi] ? i : mi);
    }
    vector<long long int> b(n);
    b[0] = a[mi];
    a[mi] = 0;
    long long int x = b[0];
    for (long long int i = 1; i <= n - 1; i++) {
      long long int in = 0, mx = 0;
      for (long long int j = 0; j < n; j++)
        if (a[j] && gcd(a[j], x) > mx) {
          mx = gcd(a[j], x);
          in = j;
        }
      b[i] = a[in];
      x = mx;
      a[in] = 0;
    }
    for (long long int i = 0; i < n; i++) cout << b[i] << ' ';
    cout << '\n';
  }
}
