#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<long, long>;
struct fast_ios {
  fast_ios() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
  }
} fast_ios_;
int solve() {
  int n;
  cin >> n;
  vector<int> dv, pri;
  dv.push_back(1);
  int nn = n;
  for (int i = 2; i * i <= nn; ++i) {
    if (n % i == 0) {
      pri.push_back(i);
      int v = 1;
      int sz = dv.size();
      while (n % i == 0) {
        n /= i;
        v *= i;
        for (int i = 0; i < sz; ++i) {
          dv.push_back(dv[i] * v);
        }
      }
    }
  }
  if (n != 1) {
    pri.push_back(n);
    int sz = dv.size();
    for (int i = 0; i < sz; ++i) {
      dv.push_back(dv[i] * n);
    }
  }
  int psz = pri.size();
  vector<int> con(psz);
  map<int, bool> vis;
  if (psz == 1) {
    for (int i = 1; i < dv.size(); ++i) {
      printf("%d ", dv[i]);
    }
    return puts("\n0");
  }
  if (psz == 2 && dv.size() == 4) {
    for (int i = 1; i < dv.size(); ++i) {
      printf("%d ", dv[i]);
    }
    return puts("\n1");
  }
  for (int i = 0; i < psz; ++i) {
    int a = pri[i];
    int b = pri[(i + 1) % psz];
    for (int j = 0; j < dv.size(); ++j) {
      if (!vis[dv[j]] && dv[j] % a == 0 && dv[j] % b == 0) {
        vis[dv[j]] = true;
        con[i] = dv[j];
        break;
      }
    }
  }
  for (int i = 0; i < psz; ++i) {
    printf("%d ", pri[i]);
    vis[pri[i]] = true;
    for (int j = 0; j < dv.size(); ++j) {
      if (!vis[dv[j]] && dv[j] % pri[i] == 0) {
        vis[dv[j]] = true;
        printf("%d ", dv[j]);
      }
    }
    printf("%d ", con[i]);
  }
  puts("\n0");
  return 0;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
