#include <bits/stdc++.h>
using namespace std;
mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count());
vector<long long> getp(long long n) {
  vector<long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans.push_back(i);
      while (n % i == 0) n /= i;
    }
  }
  if (n > 1) ans.push_back(n);
  return ans;
}
set<long long> getd(long long n) {
  set<long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      long long fir = i, sec = n / i;
      ans.insert(fir);
      ans.insert(sec);
    }
  }
  ans.insert(n);
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    auto p = getp(n);
    auto d = getd(n);
    for (auto &prost : p) d.erase(prost);
    if (p.size() == 1) {
      long long cur = p[0];
      while (n % cur == 0) {
        cout << cur << " ";
        cur *= p[0];
      }
      cout << '\n' << 0;
    } else if (p.size() == 2) {
      if (p[0] * p[1] == n)
        cout << p[0] << " " << p[1] << " " << n << '\n' << 1;
      else {
        d.erase(p[0] * p[1]);
        d.erase(n);
        vector<long long> ans;
        ans.push_back(p[0] * p[1]);
        ans.push_back(p[0]);
        vector<long long> add;
        for (auto &i : d)
          if (i % p[0] == 0) add.push_back(i);
        for (auto &i : add) {
          d.erase(i);
          ans.push_back(i);
        }
        ans.push_back(n);
        ans.push_back(p[1]);
        add.clear();
        for (auto &i : d)
          if (i % p[1] == 0) add.push_back(i);
        for (auto &i : add) {
          d.erase(i);
          ans.push_back(i);
        }
        for (auto &i : ans) cout << i << " ";
        cout << '\n' << 0;
      }
    } else {
      vector<long long> ans, mult;
      for (long long i = 0; i < p.size(); i++) {
        long long nxt = (i + 1) % p.size();
        mult.push_back(p[i] * p[nxt]);
      }
      for (auto &i : mult) d.erase(i);
      long long j = 0;
      for (auto &prost : p) {
        ans.push_back(prost);
        vector<long long> add;
        for (auto &i : d)
          if (i % prost == 0) add.push_back(i);
        for (auto &i : add) {
          d.erase(i);
          ans.push_back(i);
        }
        ans.push_back(mult[j++]);
      }
      for (auto &i : ans) cout << i << " ";
      cout << '\n' << 0;
    }
    cout << '\n';
  }
  return 0;
}
