#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b > a) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) cin >> arr[i];
    sort(arr.begin(), arr.end());
    vector<int> b(n);
    int sofar = 0;
    for (int i = 0; i < n; i++) {
      int best = 0;
      int index = -1;
      for (int j = 0; j < n; j++) {
        if (arr[j] > 0) {
          if (gcd(sofar, arr[j]) > best) {
            best = gcd(sofar, arr[j]);
            index = j;
          }
        }
      }
      b[i] = arr[index];
      sofar = gcd(sofar, arr[index]);
      arr[index] *= -1;
    }
    for (int i = 0; i < n; i++) cout << b[i] << " ";
    cout << endl;
  }
}
