#include<bits/stdc++.h>
#define pb push_back
#define en '\n'
#define bi bitset<maxn>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define ull unsigned long long
#define ll long long
//const ull mod=1e9+7;
const ull maxn=2e5+5;
const ull inf=0x3f3f3f3f;
template <class T> void rd( T &x){
x=0;
T f=1;
char s;
s=getchar();
while(!isdigit(s)){
if(s=='-')f=-1;
s=getchar();
}
while(isdigit(s)){
x=x*10+s-'0';
s=getchar();
}
x*=f;
}
ll trie[maxn][26];
ll tot;
ll cnt[maxn];
ll ed[maxn];
void insert(char *str){
ll len=strlen(str),p=1;
for(ll k=0;k<len;k++){
ll ch=str[k]-'a';
if(trie[p][ch]==0)trie[p][ch]=++tot;
p=trie[p][ch];
ed[p]+=1;
}
//ed[p]=1;
}
ll ask(char *str){
ll len=strlen(str);
ll p=1;
//string res="";
for(ll k=0;k<len;k++){
// if(trie[p][str[k]-'a']==0)return 0;
p=trie[p][str[k]-'a'];
//res+=str[k];
if(ed[p]==1){
return k;
}
}
//return ed[p];
}
char s[1005][25];
ll run(ll &x,ll p){
ll res=0;
while(x%p==0){
res+=1;
x/=p;
}return res;
}
ll n,p;
void rep(ll &x,ll pp){
x%=pp;
x+=pp;
x%=pp;
}
ll mul(ll a,ll b, ll mod){
ll res=0;
a%=mod;
while(b){
if(b&1)
res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res%mod;
}
ll pow_m(ll a,ll b){
ll res=1;
while(b){
if(b&1){
res=mul(res,a,n);//res*a;
rep(res,n);
}
b>>=1;
a=mul(a,a,n);//a*a;
rep(a,n);
}
return res;
}
bool isp(ll x){
ll tt=sqrt(x*1.0);
for(ll i=2;i*i<=x;i++){
if(x%i==0)return 0;
}
return 1;
}
signed main(){
#ifdef local
freopen("input.txt", "r", stdin);
#endif
//getchar();
ll T;
cin>>T;
while(T--){
rd(n);
ll tem;
for(ll i=n-1;i;--i){
if(isp(i)){
tem=i;
break;
}
}
ll res=1;
for(ll i=tem+1;i<=n-1;i++){
res=mul(res,i,n);//(res%n)*(i%n);
rep(res,n);
}
ll ans=mul(n-1,pow_m(res,n-2),n);//(n-1)*pow_m(res,n-2);
rep(ans,n);
cout<<ans<<en;
}
return 0;
}