#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int close_sycn = (ios::sync_with_stdio(false), cin.tie(0), 0);
vector<int> fun(int x, vector<int>& v) {
  vector<int> res;
  for (auto& p : v) {
    if (x % p == 0) res.push_back(p);
  }
  sort(res.rbegin(), res.rend());
  return res;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, x;
    cin >> n;
    vector<int> v(n), temp;
    for (int i = 1000; i > 0; i--) temp.push_back(i);
    for (int i = 0; i < n; i++) cin >> v[i];
    int m = *max_element(v.begin(), v.end());
    auto t = fun(m, temp);
    vector<pair<vector<int>, int>> a;
    for (int i = 0; i < n; i++) {
      a.push_back({fun(v[i], t), v[i]});
    }
    sort(a.rbegin(), a.rend());
    for (int i = 0; i < n; i++) cout << a[i].second << ' ';
    cout << '\n';
  }
  return 0;
}
