#include <bits/stdc++.h>
using namespace std;
int g[1005][1005];
int a[1005];
int vis[1005];
int b[1005];
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
void init() {
  for (int i = 1; i <= 1000; i++) {
    for (int j = i; j <= 1000; j++) {
      g[i][j] = gcd(i, j);
    }
  }
}
int cmp(int A, int B) { return A > B; }
int main() {
  init();
  int t;
  scanf("%d", &t);
  for (int ti = 1; ti <= t; ti++) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      vis[i] = 0;
    }
    sort(a + 1, a + n + 1, cmp);
    vis[1] = 1;
    int num = 0;
    int flag = 1;
    b[num++] = 1;
    int ts = a[1];
    for (int i = 1; i <= n;) {
      int maxs = 0;
      int flags = 1;
      for (int j = 1; j <= n; j++) {
        if (vis[j]) {
          continue;
        }
        if (g[min(a[j], ts)][max(a[j], ts)] > maxs) {
          maxs = g[min(a[j], ts)][max(a[j], ts)];
          flags = j;
        }
      }
      if (maxs == 1 || maxs == 0) {
        break;
      }
      ts = maxs;
      flag = flags;
      b[num++] = flag;
      vis[flag] = 1;
      i = flag;
    }
    for (int i = 0; i < num; i++) {
      printf("%d ", a[b[i]]);
    }
    for (int i = 1; i <= n; i++) {
      if (!vis[i]) {
        printf("%d ", a[i]);
      }
    }
    printf("\n");
  }
  return 0;
}
