#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll multi(ll a,ll b,ll c) {
ll ans = 0; a %= c;
while (b) {
if (b & 1) ans = (ans + a) % c;
b >>= 1; a = (a + a) % c;
}
return ans;
}
ll modular_exp(ll a,ll m,ll n) {
if (m == 0) return 1;
if (m == 1) return a%n;
ll w = modular_exp(a, m/2, n); w = multi(w, w, n);
if (m & 1) w =multi(w, a, n);
return w;
}
bool Miller_Rabin(ll n) {
srand(time(NULL));
if (n == 2) return true;
for (int i = 0; i < 10; ++i) {
int a = rand() % (n-2) + 2;
if (modular_exp(a, n, n) != a) return false;
}
return true;
}
void extend_gcd(ll a,ll b,ll &x,ll &y) {
if (b == 0) {
x = 1; y = 0; return;
}
extend_gcd(b, a%b, y, x);
y -= (a/b) * x;
}
ll inv(ll a,ll b) {
ll x, y; extend_gcd(a, b, x, y);
return (x + b) % b;
}
int main() {
int T; cin >> T;
while (T--) {
ll P; scanf("%lld",&P);
ll Q = P - 1, ans = P - 1;
while (!Miller_Rabin(Q)) Q--;
for (ll i = Q + 1; i <= P - 1; ++i) {
ll tmp = inv(i, P);
ans = multi(ans, tmp, P);
}
printf("%lld\n",ans);
}
return 0;
}