#include <bits/stdc++.h>
using namespace std;
int T;
int n;
int a[1005];
int G[1005][1005];
bool vis[1005];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    int tmp = 0;
    a[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (a[i] > a[tmp]) {
        tmp = i;
      }
      vis[i] = false;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        G[i][j] = gcd(a[i], a[j]);
      }
    }
    printf("%d", a[tmp]);
    vis[tmp] = true;
    for (int i = 2; i <= n; i++) {
      int x = 0;
      int y = 0;
      for (int j = 1; j <= n; j++) {
        if (vis[j]) continue;
        if (j == tmp) continue;
        if (G[tmp][j] > x) {
          x = G[tmp][j];
          y = j;
        }
      }
      printf(" %d", a[y]);
      vis[y] = true;
      tmp = y;
    }
    printf("\n");
  }
}
