#include <bits/stdc++.h>
const double eps = 1e-7, PI = 3.1415926;
const int N = 1e6 + 10;
using namespace std;
int n, q, m, k, x, y, a[N], mx = -1, mn = 1e9, sum;
string s, s1, s2;
map<int, int> mp;
vector<int> vec, dev;
vector<int> f(int x) {
  vector<int> ret;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) ret.push_back(i);
    while (x % i == 0) {
      x /= i;
    }
  }
  if (x > 1) ret.push_back(x);
  return ret;
}
vector<int> g(int x) {
  vector<int> ret;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      ret.push_back(i);
      if (i * i != x) ret.push_back(x / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
int main() {
  cin >> q;
  while (q--) {
    cin >> n;
    vec = f(n);
    dev = g(n);
    mp.clear();
    if (vec.size() == 2 && vec[0] * vec[1] == n) {
      cout << vec[0] << " " << vec[1] << " " << n << endl;
      cout << 1 << endl;
      continue;
    }
    for (int i = 0; i < vec.size(); i++) {
      for (int j = 0; j < dev.size(); j++) {
        if (dev[j] % vec[i] == 0 &&
            (i == vec.size() - 1 || dev[j] != vec[i] * vec[i + 1]) &&
            !mp[dev[j]]) {
          mp[dev[j]] = 1;
          cout << dev[j] << " ";
        }
      }
      if (i != vec.size() - 1) {
        cout << vec[i] * vec[i + 1] << " ";
        mp[vec[i] * vec[i + 1]] = 1;
      }
    }
    cout << n << endl;
    cout << 0 << endl;
  }
  return 0;
}
