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