#include <bits/stdc++.h>
using namespace std;
int n, qt;
vector<int> c, d;
map<int, int> ma;
int main() {
  scanf("%d", &qt);
  while (qt--) {
    scanf("%d", &n);
    int t = n;
    c.clear();
    d.clear();
    ma.clear();
    for (int i = 2; i <= sqrt(t); i++) {
      if (t % i == 0) {
        ma[i] = 1;
        while (t % i == 0) t /= i;
        c.push_back(i);
      }
    }
    if (t > 1) c.push_back(t), ma[t] = 1;
    for (int i = 2; i <= sqrt(n); i++) {
      if (n % i == 0) {
        if (ma[i] == 0) d.push_back(i);
        if (n / i != i && ma[n / i] == 0) d.push_back(n / i);
      }
    }
    d.push_back(n);
    if (c.size() == 2 && c[0] * c[1] == n) {
      t = c.size();
      for (int i = 0; i < t; i++) printf("%d ", c[i]);
      t = d.size();
      for (int i = 0; i < t; i++) printf("%d ", d[i]);
      printf("\n1\n");
    } else {
      int lc = c.size(), ld = d.size();
      ma.clear();
      for (int i = 0; i < lc; i++) ma[c[i] * c[(i + 1) % lc]] = 1;
      for (int i = 0; i < lc; i++) {
        printf("%d ", c[i]);
        for (int j = 0; j < ld; j++) {
          if (d[j] % c[i] == 0 && !ma[d[j]]) {
            printf("%d ", d[j]);
            ma[d[j]] = 1;
          }
        }
        if (lc == 2 && i == 0) continue;
        printf("%d ", c[i] * c[(i + 1) % lc]);
      }
      printf("\n0\n");
    }
  }
  return 0;
}
