#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 100;
const int mod = 1e9 + 7;
const int maxe = 1e6 + 100;
const int inf = 1e9 + 10;
inline int rdii() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
inline long long rdll() {
  long long sgn = 1;
  long long cnt = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') sgn = -sgn;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    cnt = cnt * 10 + (ch - '0');
    ch = getchar();
  }
  return sgn * cnt;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
int a[maxn];
int t, n;
bool cmp1(int x, int y) { return x > y; }
bool cmp2(int x, int y) { return gcd(x, a[1]) > gcd(y, a[1]); }
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n, cmp1);
    sort(a + 2, a + 1 + n, cmp2);
    for (int i = 1; i <= n; i++) printf("%d ", a[i]);
    printf("\n");
  }
  return 0;
}
