#include <bits/stdc++.h>
int gcd(int a, int b) {
  if (!a) return 0;
  return b ? gcd(b, a % b) : a;
}
int t, n, a[1005], b[1005], ct, g, mx;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    g = ct = mx = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", a + i);
      if (a[i] > a[mx]) mx = i, g = a[mx];
    }
    for (int i = 0; i < n; i++) {
      b[i] = a[mx];
      a[mx] = 0;
      for (int j = 0; j < n; j++)
        if (gcd(a[j], g) > gcd(a[mx], g)) mx = j;
      g = gcd(g, a[mx]);
    }
    for (int i = 0; i < n; i++) printf("%d ", b[i]);
    puts("");
  }
  return 0;
}
