#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return a > b ? a = b, true : false;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, true : false;
}
template <typename T>
struct IsC {
  template <typename C>
  static char test(typename C::const_iterator*);
  template <typename C>
  static int test(...);
  static const bool value = sizeof(test<T>(0)) == sizeof(char);
};
template <>
struct IsC<string> {
  static const bool value = false;
};
template <typename C>
typename enable_if<IsC<C>::value, ostream&>::type operator<<(ostream&,
                                                             const C&);
template <typename T1, typename T2>
ostream& operator<<(ostream&, const pair<T1, T2>&);
template <typename... T>
using TS = tuple_size<tuple<T...>>;
template <size_t idx, typename... T>
typename enable_if<TS<T...>::value == idx + 1, ostream&>::type operator<<(
    ostream& o, const tuple<T...>& t) {
  return o << ", " << get<idx>(t) << ')';
}
template <size_t idx, typename... T>
typename enable_if<0 < idx && idx + 1 < TS<T...>::value, ostream&>::type
operator<<(ostream& o, const tuple<T...>& t) {
  return operator<<<idx + 1>(o << ", " << get<idx>(t), t);
}
template <size_t idx = 0, typename... T>
typename enable_if<1 < TS<T...>::value && !idx, ostream&>::type operator<<(
    ostream& o, const tuple<T...>& t) {
  return operator<<<idx + 1>(o << '(' << get<idx>(t), t);
}
template <typename T>
ostream& operator<<(ostream& o, const tuple<T>& t) {
  return o << get<0>(t);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p) {
  return o << make_tuple(p.first, p.second);
}
template <typename C>
typename enable_if<IsC<C>::value, ostream&>::type operator<<(ostream& o,
                                                             const C& c) {
  o << '[';
  for (auto it = c.cbegin(); it != c.cend(); ++it)
    o << *it << (next(it) != c.cend() ? ", " : "");
  return o << ']';
}
template <typename T>
void tprint(vector<vector<T>>& v, size_t width = 0, ostream& o = cerr) {
  if (!0) return;
  for (const auto& vv : v) {
    for (const auto& i : vv) o << setw(width) << i;
    o << endl;
  }
}
const int N = 5e4;
int lp[N + 1];
vector<int> pr;
void sieve() {
  for (auto i = (2); (i) < (N + 1); ++(i)) {
    if (!lp[i]) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (int j = 0; j < (int)(pr).size() && pr[j] <= lp[i] && i * pr[j] <= N;
         ++j)
      lp[i * pr[j]] = pr[j];
  }
}
bool isPrime(int n) {
  if (n < N) return lp[n] == n;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> pdivs;
  vector<int> divs;
  for (int i = 0, nn = n; i < (int)(pr).size() || nn > 1; ++i) {
    if (i == (int)(pr).size()) {
      pdivs.emplace_back(nn, 1);
      break;
    }
    int cnt = 0;
    for (; nn % pr[i] == 0; nn /= pr[i]) {
      ++cnt;
    }
    if (cnt) {
      pdivs.emplace_back(pr[i], cnt);
    }
  }
  if (0)
    std::cerr << " ["
              << "pdivs"
                 ": "
              << make_tuple(pdivs) << "] " << endl;
  if ((int)(pdivs).size() == 2 && pdivs[0].second == 1 &&
      pdivs[1].second == 1) {
    cout << n << ' ' << pdivs[0].first << ' ' << pdivs[1].first << endl;
    cout << 1 << endl;
    return;
  }
  vector<vector<int>> ex((int)(pdivs).size());
  vector<int> skiped;
  for (vector<int> idx((int)(pdivs).size(), 0);;) {
    ++idx.back();
    for (int j = (int)(pdivs).size() - 1; j > 0 && idx[j] > pdivs[j].second;
         --j) {
      idx[j] = 0;
      ++idx[j - 1];
    }
    if (idx[0] > pdivs[0].second) {
      break;
    }
    if (0)
      std::cerr << " ["
                << "idx"
                   ": "
                << make_tuple(idx) << "] " << endl;
    int val = 1;
    int last = -1;
    int cnt = 0;
    for (auto j = (0); (j) < (((int)(pdivs).size())); ++(j)) {
      if (idx[j]) {
        last = j;
        ++cnt;
      }
      for (auto _ = (0); (_) < ((idx[j])); ++(_)) {
        val *= pdivs[j].first;
      }
    }
    if (cnt == 2 &&
        (pdivs[last].first * pdivs[last - 1].first == val ||
         pdivs[last].first * pdivs[(last + 1) % (int)(pdivs).size()].first ==
             val)) {
      skiped.push_back(val);
    } else {
      ex[last].push_back(val);
    }
  }
  if (0)
    std::cerr << " ["
              << "skiped"
                 ": "
              << make_tuple(skiped) << "] " << endl;
  if (0)
    std::cerr << " ["
              << "ex"
                 ": "
              << make_tuple(ex) << "] " << endl;
  if ((int)(ex).size() == 1) {
    for (int i : ex[0]) {
      cout << i << ' ';
    }
  } else if ((int)(ex).size() == 2) {
    for (int i : ex[0]) {
      cout << i << ' ';
    }
    cout << ex[0][0] * ex[1][0] << ' ';
    for (int i : ex[1]) {
      cout << i << ' ';
    }
  } else {
    for (auto i = (0); (i) < (((int)(ex).size())); ++(i)) {
      for (int j : ex[i]) {
        cout << j << ' ';
      }
      cout << ex[i][0] * ex[(i + 1) % (int)(ex).size()][0] << ' ';
    }
  }
  cout << endl << 0 << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  sieve();
  int tt = 1;
  cin >> tt;
  for (auto t = (1); (t) < (tt + 1); ++(t)) {
    solve();
  }
  return 0;
}
