#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
long long Bpow(long long a, long long s) {
  if (s == 0) {
    return 1;
  }
  long long u = Bpow(a, s / 2);
  if (s & 1)
    return u * u % mod * a % mod;
  else
    return u * u % mod;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int m = n;
    vector<int> p;
    vector<int> h(100);
    for (int i = 2; i * i <= m; i++) {
      if (m % i == 0) {
        p.push_back(i);
        while (m % i == 0) {
          m /= i;
          h[p.size() - 1]++;
        }
      }
    }
    if (m > 1) {
      p.push_back(m);
      h[p.size() - 1] = 1;
    }
    vector<int> del;
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        del.push_back(i);
        if (i * i != n) del.push_back(n / i);
      }
    }
    del.push_back(n);
    vector<vector<int>> k(p.size());
    for (int i = 0; i < del.size(); i++) {
      for (int j = 0; j < p.size(); j++) {
        if (del[i] % p[j] == 0) {
          k[j].push_back(del[i]);
        }
      }
    }
    if (p.size() == 1) {
      for (int i = 0; i < del.size(); i++) {
        cout << del[i] << " ";
      }
      cout << "\n0\n";
      continue;
    }
    map<int, int> used;
    if (p.size() == 2) {
      if (h[0] == 1 && h[1] == 1) {
        cout << p[0] << " " << p[1] << " " << p[0] * p[1] << "\n1\n";
        continue;
      }
      if (h[0] == 1) {
        swap(k[0], k[1]);
        swap(p[0], p[1]);
        swap(h[0], h[1]);
      }
      used[p[0]] = 1;
      int last = p[0] * p[1];
      used[last] = 1;
      cout << p[0] << " ";
      for (int i = 0; i < k[0].size(); i++) {
        if (!used[k[0][i]]) {
          used[k[0][i]] = 1;
          cout << k[0][i] << " ";
        }
      }
      for (int i = 0; i < k[1].size(); i++) {
        if (!used[k[1][i]]) {
          used[k[1][i]] = 1;
          cout << k[1][i] << " ";
        }
      }
      cout << last << "\n0\n";
      continue;
    }
    used[p[0] * p.back()] = 1;
    for (int i = 0; i < p.size(); i++) {
      used[p[i] * p[(i + 1) % p.size()]] = 1;
      for (int j = 0; j < k[i].size(); j++) {
        if (!used[k[i][j]]) {
          used[k[i][j]] = 1;
          cout << k[i][j] << " ";
        }
      }
      cout << p[i] * p[(i + 1) % p.size()] << " ";
    }
    cout << "\n0\n";
  }
}
