#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t;
int PrimesInTheArray[N];
template <class fil>
inline void read(fil &ret) {
  ret = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch) && ch ^ '-') ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (isdigit(ch)) {
    ret = (ret << 1) + (ret << 3) + (ch ^ '0');
    ch = getchar();
  }
  ret *= f;
  return;
}
template <class fil>
inline void write(fil ret) {
  if (ret < 0) ret = -ret, putchar('-');
  if (ret > 9) write(ret / 10);
  putchar(ret % 10 + '0');
  return;
}
int main() {
  read(t);
  while (t--) {
    int n;
    read(n);
    int cntforlastprimes = 0;
    int tmp = n;
    vector<int> FindRoutes;
    FindRoutes.clear();
    for (register int i = 2; i * i <= tmp; ++i) {
      if (tmp % i == 0) {
        PrimesInTheArray[++cntforlastprimes] = i;
        while (tmp % i == 0) tmp /= i;
      }
    }
    if (tmp > 1) PrimesInTheArray[++cntforlastprimes] = tmp;
    for (register int i = 2; i * i <= n; ++i) {
      if (n % i == 0) {
        FindRoutes.push_back(i);
        if (i ^ (n / i)) FindRoutes.push_back(n / i);
      }
    }
    FindRoutes.push_back(n);
    if (cntforlastprimes == 1) {
      for (register int i = 0; i < FindRoutes.size(); ++i)
        cout << FindRoutes[i] << ' ';
      cout << endl << 0 << endl;
      continue;
    }
    map<int, int> Map;
    if (cntforlastprimes == 2 &&
        PrimesInTheArray[1] * PrimesInTheArray[2] == n) {
      cout << PrimesInTheArray[1] << ' ' << n << ' ' << PrimesInTheArray[2]
           << endl
           << 1 << endl;
      continue;
    } else {
      Map[n] = 1;
      for (register int i = 2; i <= cntforlastprimes; ++i)
        Map[PrimesInTheArray[i - 1] * PrimesInTheArray[i]] = 114514;
      for (register int i = 1; i <= cntforlastprimes; ++i) {
        for (register int j = 0; j < FindRoutes.size(); ++j) {
          if (FindRoutes[j] % PrimesInTheArray[i] == 0 &&
              Map[FindRoutes[j]] == 0)
            cout << FindRoutes[j] << ' ', Map[FindRoutes[j]] = 114514;
        }
        if (i < cntforlastprimes)
          cout << PrimesInTheArray[i + 1] * PrimesInTheArray[i] << ' ';
        else
          cout << n << ' ';
      }
      cout << endl << 0 << endl;
    }
  }
  return 0;
}
