#include <bits/stdc++.h>
using namespace std;
const int MAXC = 1e9;
int t, n;
int l, r, tmp;
int now;
vector<int> arr, ans, D;
signed main() {
  cin >> t;
  while (t--) {
    cin >> n;
    arr.clear();
    ans.clear();
    D.clear();
    ans.push_back(n);
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        arr.push_back(i);
        if (i != n / i) arr.push_back(n / i);
      }
    }
    tmp = n;
    for (int i = 2; i * i <= MAXC && tmp > 1; i++) {
      if (tmp % i == 0) {
        D.push_back(i);
        while (tmp % i == 0) tmp /= i;
      }
    }
    if (tmp > 1) D.push_back(tmp);
    if (D.size() == 2 && n == D[0] * D[1]) {
      cout << n << " " << D[0] << " " << D[1] << "\n";
      cout << 1 << "\n";
      continue;
    }
    now = 0;
    l = 0, r = arr.size();
    while (l < r) {
      sort(arr.begin() + l, arr.begin() + r, [](const int a, const int b) {
        return ((a % D[now] != b % D[now]) ? (a % D[now] < b % D[now])
                                           : (a < b));
      });
      ans.push_back(arr[l]);
      tmp = -1;
      while (l + 1 < r && arr[l + 1] % D[now] == 0) {
        l++;
        if (now + 1 < D.size() && arr[l] % D[now + 1] == 0 && tmp == -1)
          tmp = arr[l];
        else
          ans.push_back(arr[l]);
      }
      if (tmp != -1) ans.push_back(tmp);
      l++, now++;
    }
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << " \n"[i == ans.size() - 1];
    cout << 0 << "\n";
  }
}
