#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, NN = 2e3 + 10, INF = 0x3f3f3f3f, LEN = 20;
const long long MOD = 1e9 + 7;
const unsigned long long seed = 31;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
vector<int> vec;
int n, sum, temp;
int a[N];
bool vis[N];
void init() {
  vec.clear();
  memset(vis, false, sizeof vis);
}
int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); }
bool cmp(int x, int y) { return x > y; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    init();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n, cmp);
    sum = 1;
    temp = a[1];
    vis[1] = true;
    vec.push_back(a[1]);
    while (sum < n) {
      int id, max = -INF;
      for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        int cnt = gcd(temp, a[i]);
        if (cnt > max) {
          id = i;
          max = cnt;
        }
      }
      ++sum;
      vis[id] = true;
      vec.push_back(a[id]);
      temp = gcd(temp, a[id]);
    }
    bool flag = false;
    vector<int>::iterator it;
    for (it = vec.begin(); it != vec.end(); it++) {
      if (!flag) {
        printf("%d", *it);
        flag = true;
      } else
        printf(" %d", *it);
    }
    puts("");
  }
}
