#include <bits/stdc++.h>
using namespace std;
void cout_vector(vector<int> v) {
  for (long long unsigned i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
int sum_vector(vector<int> v) { return accumulate(v.begin(), v.end(), 0); }
void sort_vector(vector<int> &v) { sort(v.begin(), v.end()); }
void sort_comp(vector<int> &v, bool func(int, int)) {
  sort(v.begin(), v.end(), func);
}
bool comp(int a, int b) {
  if (a % 2 == 0 && b % 2) return true;
  if (b % 2 == 0 && a % 2) return false;
  return a < b;
}
long long gcd(long long a, long long b) {
  while (a) {
    long long temp = a;
    a = b % a;
    b = temp;
  }
  return b;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
string binary(long long num) {
  string ans = "";
  do {
    ans = to_string(num % 2) + ans;
    num /= 2;
  } while (num);
  return ans;
}
void solve() {
  int n;
  cin >> n;
  vector<int> divisors = {n};
  vector<int> fac;
  vector<bool> used = {1};
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      divisors.push_back(i);
      used.push_back(0);
      if (n != i * i) {
        divisors.push_back(n / i);
        used.push_back(0);
      }
    }
  }
  fac.push_back(divisors[1]);
  int rest = n / fac[0];
  while (rest != 1) {
    for (int i = 2; i * i <= rest + 3; i++) {
      if (rest % i == 0) {
        fac.push_back(i);
        rest /= i;
        break;
      }
      if ((i + 1) * (i + 1) > rest) {
        fac.push_back(rest);
        rest = 1;
      }
    }
  }
  sort_vector(fac);
  vector<int> uni_fac = {fac[0]};
  int last = fac[0];
  for (int num : fac) {
    if (num == last)
      ;
    else {
      last = num;
      uni_fac.push_back(last);
    }
  }
  uni_fac.push_back(uni_fac[0]);
  if (fac.size() == 2 && (fac[0] != fac[1])) {
    cout_vector(divisors);
    cout << 1 << endl;
  } else {
    for (int i = 0; i < int(uni_fac.size()) - 1; i++) {
      cout << uni_fac[i] << " ";
      for (int j = 0; j < int(divisors.size()); j++) {
        if ((!used[j]) && (divisors[j] % uni_fac[i] == 0)) {
          if (divisors[j] != uni_fac[i] * uni_fac[i + 1] &&
              divisors[j] != uni_fac[i]) {
            cout << divisors[j] << " ";
          }
          used[j] = 1;
        }
      }
      if ((i == 0 || (i != int(uni_fac.size()) - 2)) &&
          divisors[0] != uni_fac[i] * uni_fac[i + 1])
        cout << uni_fac[i] * uni_fac[i + 1] << " ";
    }
    cout << divisors[0];
    cout << endl << 0 << endl;
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
