#include <bits/stdc++.h>
using namespace std;
int prime[110000], num;
bool bn[110000];
void findPrime(int n) {
  int i, j;
  memset(bn, 1, sizeof(bn));
  prime[0] = 2;
  for (i = 2; i < n; i++) {
    if (bn[i]) prime[num++] = i;
    for (j = 0; j < num && i * prime[j] < n; j++) {
      bn[i * prime[j]] = 0;
      if (i % prime[j] == 0) break;
    }
  }
}
int an[110], factorNum;
int pn[110];
void primeFactorization(int a) {
  int i, j;
  memset(pn, 0, sizeof(pn));
  for (i = 0; i < num; i++) {
    int t = prime[i];
    if (a % t == 0) an[++factorNum] = t;
    while (a % t == 0) {
      pn[factorNum]++;
      a /= t;
    }
    if (t * t >= a) break;
  }
  if (a != 1) {
    an[++factorNum] = a;
    pn[factorNum] = 1;
  }
}
set<int> st;
int ansA[20][110000];
int ansAB[20][110000];
void dfs(int k, int ans) {
  int i, j, t;
  if (k > factorNum) {
    st.insert(ans);
    return;
  }
  for (i = 0, t = 1; i <= pn[k]; i++, t *= an[k]) dfs(k + 1, ans * t);
}
int main() {
  num = 0;
  findPrime(110000);
  int z;
  cin >> z;
  while (z--) {
    int n;
    cin >> n;
    int i, j, k;
    int ans;
    factorNum = 0;
    primeFactorization(n);
    if (factorNum == 1) {
      ans = 1;
      for (i = 1; i <= pn[1]; i++) {
        ans *= an[1];
        printf("%d ", ans);
      }
      printf("\n0\n");
    } else if (factorNum == 2) {
      if (pn[1] == 1 && pn[2] == 1) {
        printf("%d %d %d\n1\n", an[1], an[2], an[1] * an[2]);
      } else {
        ans = 1;
        for (i = 1; i <= pn[1]; i++) {
          ans *= an[1];
          printf("%d ", ans);
        }
        printf("%d ", an[1] * an[2]);
        ans = 1;
        for (i = 1; i <= pn[2]; i++) {
          ans *= an[2];
          printf("%d ", ans);
        }
        for (i = 1; i <= pn[1]; i++) {
          for (j = 1; j <= pn[2]; j++) {
            if (i == 1 && j == 1) continue;
            ans = 1;
            for (k = 1; k <= i; k++) ans *= an[1];
            for (k = 1; k <= j; k++) ans *= an[2];
            printf("%d ", ans);
          }
        }
        printf("\n0\n");
      }
    } else {
      dfs(1, 1);
      for (i = 1; i <= factorNum; i++) {
        ansA[i][0] = 0;
        ans = 1;
        for (j = 1; j <= pn[i]; j++) {
          ans *= an[i];
          st.erase(ans);
          ansA[i][++ansA[i][0]] = ans;
        }
        int t;
        if (i != factorNum)
          t = an[i] * an[i + 1];
        else
          t = an[i] * an[1];
        ansAB[i][0] = 0;
        for (auto itr = st.begin(); itr != st.end();) {
          auto tmp = itr;
          itr++;
          if (*tmp % t == 0) {
            ansAB[i][++ansAB[i][0]] = *tmp;
            st.erase(tmp);
          }
        }
      }
      for (i = 1; i <= factorNum; i++) {
        for (auto itr = st.begin(); itr != st.end();) {
          auto tmp = itr;
          itr++;
          if (*tmp % an[i] == 0) {
            ansA[i][++ansA[i][0]] = *tmp;
            st.erase(tmp);
          }
        }
      }
      int sum = 0;
      for (i = 1; i <= factorNum; i++) {
        for (j = 1; j <= ansA[i][0]; j++) printf("%d ", ansA[i][j]);
        for (j = 1; j <= ansAB[i][0]; j++) printf("%d ", ansAB[i][j]);
        sum += ansA[i][0] + ansAB[i][0];
      }
      printf("\n0\n");
    }
  }
  return 0;
}
