#include <bits/stdc++.h>
using namespace std;
template <typename T, typename F>
string to_string(pair<T, F> p);
template <typename T, typename F, typename Tr>
string to_string(tuple<T, F, Tr> p);
template <typename T, typename F, typename Tr, typename Th>
string to_string(tuple<T, F, Tr, Th> p);
string to_string(string s) { return '"' + s + '"'; }
string to_string(char c) { return (string) "'" + c + "'"; }
string to_string(const char* p) { return to_string((string)p); }
string to_string(bool B) { return (B ? "true" : "false"); }
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < (int)v.size(); ++i) {
    if ((int)res.size() > 1) res += ", ";
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t T>
string to_string(bitset<T> bs) {
  return bs.to_string();
}
template <typename T>
string to_string(T v) {
  string res = "{";
  for (auto& el : v) {
    if ((int)res.size() > 1) res += ", ";
    res += to_string(el);
  }
  res += "}";
  return res;
}
template <typename T, typename F>
string to_string(pair<T, F> p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template <typename T, typename F, typename Tr>
string to_string(tuple<T, F, Tr> p) {
  return '(' + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ')';
}
template <typename T, typename F, typename Tr, typename Th>
string to_string(tuple<T, F, Tr, Th> p) {
  return '(' + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ')';
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const int N = sqrt((int)1e9 + 1) + 1;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    long long tn = n;
    vector<pair<long long, long long>> divs;
    for (long long i = 2; i < N; ++i) {
      if (n % i == 0) divs.emplace_back(i, 0);
      while (n % i == 0) {
        n /= i;
        ++divs.back().second;
      }
    }
    if (n > 1) {
      divs.emplace_back(n, 1);
    }
    n = tn;
    if ((long long)divs.size() == 1) {
      long long cur = divs[0].first;
      while (divs[0].second-- > 0) {
        cout << cur << ' ';
        cur *= divs[0].first;
      }
      cout << '\n' << 0 << '\n';
      continue;
    }
    if ((long long)divs.size() == 2 && divs[0].second == 1 &&
        divs[1].second == 1) {
      cout << divs[0].first << ' ' << divs[1].first << ' '
           << divs[0].first * divs[1].first << '\n';
      cout << 1 << '\n';
    } else {
      vector<long long> ans;
      map<long long, long long> vis;
      auto add = [&](long long x) {
        if (vis[x]) return;
        vis[x] = true;
        ans.push_back(x);
      };
      if ((long long)divs.size() != 2) {
        for (long long i = 0; i < n; ++i) {
          long long bef = divs[(i ? i - 1 : (long long)divs.size() - 1)].first;
          add(bef * divs[i].first);
          if (i < (long long)divs.size() - 1)
            vis[divs[i].first * divs[i + 1].first] = true;
          long long sy = divs[i].second;
          while (sy--) {
            n /= divs[i].first;
          }
          sy = divs[i].second;
          vector<long long> tmp;
          for (long long j = 1; j * j <= n; ++j) {
            if (n % j == 0) {
              tmp.push_back(j);
              tmp.push_back(n / j);
            }
          }
          if ((long long)tmp.size() > 2 &&
              tmp.back() == tmp[(long long)tmp.size() - 2])
            tmp.pop_back();
          long long curt = divs[i].first;
          for (long long j = 0; j < sy; ++j) {
            for (auto el : tmp) {
              add(el * curt);
            }
            if (j < sy - 1) curt *= divs[i].first;
          }
          if (i < (long long)divs.size() - 1)
            vis[divs[i].first * divs[i + 1].first] = false;
        }
      } else {
        for (long long i = 0; i < n; ++i) {
          long long bef = divs[(i ? i - 1 : (long long)divs.size() - 1)].first;
          add(bef * divs[i].first *
              (i == 0 ? (divs[i].second > 1 ? divs[i].first : divs[i ^ 1].first)
                      : 1));
          if (i < (long long)divs.size() - 1)
            vis[divs[i].first * divs[i + 1].first] = true;
          long long sy = divs[i].second;
          while (sy--) {
            n /= divs[i].first;
          }
          sy = divs[i].second;
          vector<long long> tmp;
          for (long long j = 1; j * j <= n; ++j) {
            if (n % j == 0) {
              tmp.push_back(j);
              tmp.push_back(n / j);
            }
          }
          if ((long long)tmp.size() > 2 &&
              tmp.back() == tmp[(long long)tmp.size() - 2])
            tmp.pop_back();
          long long curt = divs[i].first;
          for (long long j = 0; j < sy; ++j) {
            for (auto el : tmp) {
              add(el * curt);
            }
            if (j < sy - 1) curt *= divs[i].first;
          }
          if (i < (long long)divs.size() - 1)
            vis[divs[i].first * divs[i + 1].first] = false;
        }
      }
      add(divs[0].first * divs.back().first);
      for (auto el : ans) cout << el << ' ';
      cout << '\n';
      cout << 0 << '\n';
    }
  }
}
