#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
vector<int> primes;
void solve() {
  int n;
  cin >> n;
  set<int> divisors;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      divisors.insert(i);
      divisors.insert(n / i);
    }
  }
  auto prime = [&](int x) {
    if (x == 2 || x == 3) return true;
    for (int i = 2; i * i <= x; i++) {
      if (x % i == 0) return false;
    }
    return true;
  };
  if (divisors.size() == 2) {
    if (prime(*divisors.begin()) && prime(*prev(divisors.end()))) {
      for (auto d : divisors) cout << d << " ";
      cout << n << endl;
      cout << 1 << endl;
      return;
    }
  }
  divisors.insert(n);
  vector<int> primes;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      primes.push_back(i);
      while (n % i == 0) n = n / i;
    }
  }
  if (n > 1) primes.push_back(n);
  vector<int> ans;
  unordered_set<int> avail(divisors.begin(), divisors.end());
  for (int i = 0; i < primes.size(); i++) {
    int p = primes[i];
    int q = primes[(i + 1) % primes.size()];
    for (auto d : divisors) {
      if (avail.find(d) != avail.end() && d % p == 0 && d % q != 0) {
        ans.push_back(d);
        avail.erase(d);
      }
    }
    int r = p * q;
    if (avail.find(r) != avail.end()) {
      ans.push_back(r);
      avail.erase(r);
    }
  }
  for (auto d : divisors) {
    if (avail.find(d) != avail.end()) {
      ans.push_back(d);
      avail.erase(d);
    }
  }
  assert(avail.empty());
  for (auto e : ans) cout << e << " ";
  cout << endl;
  cout << 0 << endl;
}
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
