#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int maxN = 0;
  for (int i = 0; i < n; i++) {
    maxN = max(maxN, a[i]);
  }
  vector<int> vec;
  for (int i = 1; i * i <= maxN; i++) {
    if (maxN % i == 0) vec.push_back(i);
    if (maxN / i != i && maxN % i == 0) vec.push_back(maxN / i);
  }
  sort(vec.begin(), vec.end());
  vector<bool> printed(n, false);
  int cnt = 0;
  for (int i = vec.size() - 1; i >= 0; i--) {
    int x = vec[i];
    for (int j = 0; j < n; j++) {
      if (!printed[j] && a[j] % x == 0) {
        cout << a[j] << " ";
        printed[j] = true;
        cnt++;
      }
    }
    if (cnt == n) break;
  }
  cout << "\n";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
