#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1024;
int a[MAXN];
bool vis[MAXN];
int t, n;
vector<int> ans;
int gcd(int x, int y) {
  if (x % y == 0) return y;
  return gcd(y, x % y);
}
void init() {
  memset(a, 0, sizeof(a));
  memset(vis, 0, sizeof(vis));
  ans.clear();
}
int main() {
  scanf("%d", &t);
  while (t--) {
    init();
    scanf("%d", &n);
    int gcd_ = 0, index_ = 1;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (gcd_ < a[i]) {
        gcd_ = a[i];
        index_ = i;
      }
    }
    ans.push_back(a[index_]);
    vis[index_] = 1;
    for (int i = 2; i <= n; i++) {
      int las = gcd_;
      gcd_ = 0;
      for (int j = 1; j <= n; j++) {
        if (!vis[j]) {
          if (gcd_ < gcd(las, a[j])) {
            gcd_ = gcd(las, a[j]);
            index_ = j;
          }
        }
      }
      ans.push_back(a[index_]);
      vis[index_] = 1;
    }
    printf("%d", ans[0]);
    for (int i = 1; i < n; i++) {
      printf(" %d", ans[i]);
    }
    puts("");
  }
}
