#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1000005;
const double eps = 1e-8;
int n, m;
int T;
vector<int> primes, d;
void get_primes(int x) {
  int i, j;
  for (i = 2; i <= x / i; i++) {
    if (x % i == 0) {
      while (x % i == 0) {
        x = x / i;
      }
      primes.push_back(i);
    }
  }
  if (x > 1) primes.push_back(x);
}
void get_div(int x) {
  int i, j;
  for (i = 2; i <= x / i; i++) {
    if (x % i == 0) {
      d.push_back(i);
      if (i * i != x) {
        d.push_back(x / i);
      }
    }
  }
  d.push_back(x);
}
int main() {
  int i, j;
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    primes.clear();
    d.clear();
    get_primes(n);
    get_div(n);
    map<int, int> ma;
    if (primes.size() == 2 && d.size() == 3) {
      for (i = 0; i < d.size(); i++) {
        cout << d[i] << " ";
      }
      cout << endl << 1 << endl;
    } else {
      for (i = 0; i < primes.size(); i++) {
        int t = i + 1;
        if (t == primes.size()) {
          t = 0;
        }
        ma[primes[i] * primes[t]] = 1;
      }
      if (primes.size() == 2) {
        ma[n] = 1;
        for (i = 0; i < primes.size(); i++) {
          cout << primes[i] << " ";
          ma[primes[i]] = 1;
          int t = i + 1;
          if (t == primes.size()) {
            t = 0;
          }
          for (j = 0; j < d.size(); j++) {
            if (d[j] % primes[i] == 0 && ma.count(d[j]) == 0) {
              cout << d[j] << " ";
              ma[d[j]] = 1;
            }
          }
          if (i == primes.size() - 1)
            cout << n << " ";
          else
            cout << primes[i] * primes[t] << " ";
        }
        cout << endl << 0 << endl;
        continue;
      }
      for (i = 0; i < primes.size(); i++) {
        cout << primes[i] << " ";
        ma[primes[i]] = 1;
        int t = i + 1;
        if (t == primes.size()) {
          t = 0;
        }
        for (j = 0; j < d.size(); j++) {
          if (d[j] % primes[i] == 0 && ma.count(d[j]) == 0) {
            cout << d[j] << " ";
            ma[d[j]] = 1;
          }
        }
        cout << primes[i] * primes[t] << " ";
      }
      cout << endl << 0 << endl;
    }
  }
  return 0;
}
