#include <bits/stdc++.h>
using namespace std;
int read() {
  int tot = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    tot = tot * 10 + c - '0';
    c = getchar();
  }
  return tot * fh;
}
void getstr(char *a) {
  char c = getchar();
  int len = 0;
  while (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
           (c >= '0' && c <= '9')))
    c = getchar();
  while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
         (c >= '0' && c <= '9')) {
    a[++len] = c;
    c = getchar();
  }
}
int gcd(int x, int y) {
  while (y) {
    int z = x % y;
    x = y;
    y = z;
  }
  return x;
}
const int maxn = 2020;
int t, n, a[maxn], rem, tmp, b[maxn], c[maxn];
bool d[maxn];
int main() {
  t = read();
  while (t--) {
    n = read();
    for (int i = 1; i <= n; i++) {
      a[i] = read();
      d[i] = true;
    }
    tmp = 0;
    rem = 1;
    for (int i = 1; i <= n; i++)
      if (a[i] > tmp) {
        tmp = a[i];
        rem = i;
      }
    b[1] = a[rem];
    c[1] = a[rem];
    d[rem] = false;
    for (int i = 2; i <= n; i++) {
      tmp = 0;
      for (int j = 1; j <= n; j++)
        if (d[j] && gcd(c[i - 1], a[j]) > tmp) {
          rem = j;
          tmp = gcd(c[i - 1], a[j]);
        }
      b[i] = a[rem];
      c[i] = gcd(b[i], c[i - 1]);
      d[rem] = false;
    }
    for (int i = 1; i <= n; i++) cout << b[i] << " ";
    cout << endl;
  }
  return 0;
}
