#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long long maxn = 1000 + 10;
const unsigned long long modd = 212370440130137957ll;
int lowbit(int x) { return x & -x; }
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long quick_power(long long a, long long b, long long c) {
  long long ans = 1;
  a = a % c;
  while (b) {
    if (b & 1) ans = (ans * a) % c;
    b = b >> 1;
    a = (a * a) % c;
  }
  return ans;
}
bool isprime(int x) {
  int flag = 1;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      flag = 0;
      break;
    }
  }
  if (flag)
    return 1;
  else
    return 0;
}
int a[maxn];
int visit[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    memset(visit, 0, sizeof(visit));
    int n;
    cin >> n;
    for (int i = 0; i <= n - 1; i++) {
      cin >> a[i];
    }
    int pos, Gcd = 0;
    for (int i = 0; i <= n - 1; i++) {
      pos = -1;
      for (int j = 0; j <= n - 1; j++) {
        if (!visit[j]) {
          if (pos == -1 || gcd(Gcd, a[j]) > gcd(Gcd, a[pos])) {
            pos = j;
          }
        }
      }
      Gcd = gcd(Gcd, a[pos]);
      cout << a[pos] << " ";
      visit[pos] = 1;
    }
    cout << endl;
  }
  return 0;
}
