#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long x;
    cin >> x;
    long long t = x;
    vector<long long> p;
    set<long long> s;
    for (long long i = 2; i * i <= x; i++) {
      if (x % i == 0) {
        p.push_back(i);
        while (x % i == 0) x /= i;
      }
    }
    if (x > 1) p.push_back(x);
    sort(p.begin(), p.end());
    for (long long i = 1; i * i <= t; i++) {
      if (t % i == 0) {
        s.insert(i);
        s.insert(t / i);
      }
    }
    s.erase((long long)1);
    if (p.size() == 1) {
      for (auto i : s) {
        cout << i << " ";
      }
      cout << endl;
      cout << 0 << endl;
      continue;
    }
    if (s.size() == 3 && p.size() == 2) {
      cout << p[0] << " " << p[1] << " " << t << endl;
      cout << 1 << endl;
      continue;
    }
    int now = 1, sz = p.size(), sum = s.size();
    s.erase(t);
    a[sum] = t;
    for (int i = 0; i < sz; i++) {
      a[now] = p[i];
      now++;
      vector<long long> num;
      s.erase(p[i] * p[(i + 1) % sz]);
      s.erase(p[i]);
      for (auto j : s) {
        if (j % p[i] == 0) a[now] = j, now++, num.push_back(j);
      }
      if (now != sum) a[now] = p[i] * p[(i + 1) % sz];
      now++;
      for (auto j : num) {
        s.erase(j);
      }
    }
    for (int i = 1; i <= sum; i++) {
      cout << a[i] << " ";
    }
    cout << endl;
    cout << 0 << endl;
  }
}
