#include using namespace std; // {{{ Templates // clang-format off // Macros #define over_load_(_1,_2,_3,_4,NAME,...) NAME #define rep(...) over_load_(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__) #define rep2(i, r) for ( int i = 0; i < static_cast(r); (i) += 1) #define rep3(i, l, r) for ( int i = static_cast(l); i < static_cast(r); (i) += 1) #define rep4(i, l, r, stride) for ( int i = static_cast(l); i < static_cast(r); (i) += (stride)) #define rrep(...) over_load_(__VA_ARGS__, rrep4, rrep3, rrep2)(__VA_ARGS__) #define rrep2(i, r) for ( int i = static_cast(r) - 1; i >= 0; (i) -= 1) #define rrep3(i, l, r) for ( int i = static_cast(r) - 1; i >= static_cast(l); (i) -= 1) #define rrep4(i, l, r, stride) for ( int i = static_cast(r) - 1; i >= static_cast(l); (i) -= (stride)) #define len(x) (static_cast((x).size())) #define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x) #define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x) #define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__) // Operators template ostream &operator<<(ostream &os, const pair &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &v) { bool is_first = true; for (auto x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; } template ostream &operator<<(ostream &os, queue v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ")< ostream &operator<<(ostream &os, stack v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ") << v.top(); v.pop(); is_first=false; } return os; } template ostream &operator<<(ostream &os, const vector &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; } template ostream &operator<<(ostream &os, const vector> &v) { for (const auto &vec: v) { os << vec << '\n'; } return os; } template ostream &operator<<(ostream &os, const deque &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; } template ostream &operator<<(ostream &os, const set &v) { bool is_first = true; for (T x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; } template istream &operator>>(istream &is, vector &v) { for (T &in: v) { is >> in; } return is; } // For debug macro int find_comma_not_bracketed(string_view s){ stack bs; string lbs = "({[", rbs = ")}]"; for (size_t i = 0; i < s.size(); i++) { if (lbs.find(s[i]) != string::npos) bs.push(s[i]); if (rbs.find(s[i]) != string::npos and !bs.empty()) bs.pop(); if (s[i] == ',' and bs.empty()) return i; } return s.size(); } template void debug_function(string_view name, const T &a, Ts &&...rest) { int end = find_comma_not_bracketed(name); cerr << name.substr(0, end) << ":" << a; if constexpr (sizeof...(rest) == 0) { cerr << '\n'; } else { cerr << ' '; debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward(rest)...); } } // Functions template vector make_vector(size_t a, T b) { return vector(a, b); } template auto make_vector(size_t a, Ts... ts) { return vector(a, make_vector(ts...)); } template inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); } template inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); } // Structs struct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup; // Type aliases using ull = unsigned long long; using ll = long long; using pll = pair; using pii = pair; // Literals constexpr ll INF64 = INT64_MAX / 2; constexpr int INF32 = INT32_MAX / 2; constexpr int dy[] = { 0, 1, -1, 0, -1, 1, -1, 1 }; constexpr int dx[] = { 1, 0, 0, -1, -1, -1, 1, 1 }; constexpr int mod998244353 = 998244353; constexpr int mod1000000007 = static_cast(1e9) + 7; constexpr char newl = '\n'; // clang-format on // }}} Templates namespace math { constexpr bool is_prime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } // namespace math using namespace math; map ans; constexpr void set_answers() { constexpr int limit = 1e9; for (int p = 3; p < limit; p++) { if (not is_prime(p)) continue; ll val = 1; ll x = 0; bool found = false; bool used[p] = {}; for (int cnt = 0; cnt < p; cnt++) { if (used[val]) { ans[p] = x; found = true; break; } x++; val = (val * 2) % p; } if (not found) { ans[p] = -1; } } } int main() { set_answers(); int n; cin >> n; vector ps(n); cin >> ps; for (auto p: ps) { cout << ans[p] << newl; } }