#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
typedef long long ll, i64;
ll fmul(ll a, ll b, ll m)
{
    ll r = 0;
    for (; b; b >>= 1, a = (a << 1) % m)
        if (b & 1)
            r = (r + a) % m;
    return r;
}
ll fpow(ll a, ll b, ll m)
{
    ll r = 1;
    for (; b; b >>= 1, a = fmul(a, a, m))
        if (b & 1)
            r = fmul(r, a, m);
    return r;
}
bool miller_rabin(ll n, ll a)
{
    ll d = n - 1;
    if (n == a) return true;
    if (n & 1 ^ 1) return false;
    while (d & 1 ^ 1) d >>= 1;
    ll t = fpow(a, d, n);
    while (d != n - 1 && t != n - 1 && t != 1)
    {
        //t = t * t % n;
        t = fmul(t, t, n);
        d <<= 1;
    }
    return t == n - 1 || (d & 1) == 1;
}
bool isPrime(ll x)
{
    auto base = {2, 3, 5, 7, 11, 13, 17, 61, 24251};
    for (auto a : base)
    {
        if (x == a) return true;
        if (!miller_rabin(x, a)) return false;
    }
    return true;
}
inline ll inv(ll x, ll m) { return fpow(x, m - 2, m); }
int main()
{
    ll T, P, Q;
    scanf("%lld", &T);
    while (T--)
    {
        scanf("%lld", &P);
        if (P == 3)
            Q = 2;
        else
            for (Q = P - 2; Q > 0; Q -= 2)
                if (isPrime(Q)) break;
        ll ans = P - 1;
        for (ll x = Q + 1; x <= P - 1; x++)
            ans = fmul(ans, inv(x, P), P);
        printf("%lld\n", ans);
    }
    return 0;
}