#include <bits/stdc++.h>
using namespace std;
int T, n;
vector<int> A;
int GCD(int x, int y) {
  if (x == 0 || y == 0) return x + y;
  return GCD(y, x % y);
}
void xuly() {
  int cur;
  vector<int> res(n);
  vector<bool> flag(n);
  int temp = 0, ft = 0;
  for (int i = 0; i < n; ++i) {
    if (temp < A[i]) {
      temp = A[i];
      ft = i;
    }
  }
  res[0] = A[ft];
  flag[ft] = true;
  cur = A[ft];
  for (int i = 1; i < n; ++i) {
    temp = 0;
    ft = 0;
    for (int j = 0; j < n; ++j) {
      if (!flag[j] && GCD(cur, A[j]) > temp) {
        temp = GCD(cur, A[j]);
        ft = j;
      }
    }
    res[i] = A[ft];
    flag[ft] = true;
    cur = GCD(cur, A[ft]);
  }
  for (int i = 0; i < n; ++i) cout << res[i] << " ";
  cout << '\n';
}
void nhap() {
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i) cin >> A[i];
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    nhap();
    xuly();
  }
  return 0;
}
