#include #include #include #include #include #include #include #include #include #include #define LL long long #define LD double #define inf 10000000000000000ll using namespace std; long long gcd(long long a,long long b) { if (!b) return a; return gcd(b,a%b); } long long lcm(long long a,long long b) { long long gc=gcd(a,b); return a/gc*b; } long long to[50]; long long p1[50],p2[50],top,nd,mo[50],yu[50]; void work(int n,int a[]) { if (n==1) return; int ls=0; for (int ths=(a[1]==n)?(1):(a[1]+1);ths!=a[1];(ths==n)?(ths=1):(ths++)) { to[ths]=++ls; } for (int i=2;i<=n;i++) a[i]=to[a[i]]; work(n-1,a+1); p1[++top]=n;p2[top]=a[1]%n; } int a[50]; void exgcd(LL a,LL b,LL& x,LL& y) { if (b==0) {x=1;y=0;return;} LL px,py; exgcd(b,a%b,px,py); x=py;y=px-a/b*py; } map mp; int zuo(int n) { int ddd=1; for (int i=1;i<=n;i++) { //if ((i!=2)&&(i!=3)&&(i!=5)&&(i!=7)&&(i!=11)&&(i!=13)&&(i!=17)&&(i!=19)) continue; if (!mp.count(i)) continue; int ths; for (int d=0;d<=i;d++) { if (d==i) return -1; int js=0; for (int j=1;j<=top;j++) { int er=mp[i],s=1; while (p1[j]%(s*er)==0) s*=er; if (d%s==p2[j]%s) js++; } if (js==top) {ths=d;break;} } mo[++nd]=i;yu[nd]=ths; ddd*=i; } return ddd; } void www(int x,int n) { int d=1; while (d*x<=n) d*=x; if (d!=1) mp[d]=x; } int main() { int t;scanf("%d",&t); while (t--) { int n;scanf("%d",&n);top=0;nd=0; for (int i=1;i<=n;i++) { int w;scanf("%d",&w); a[w]=i; } mp.clear(); www(2,n);www(3,n);www(5,n);www(7,n);www(11,n);www(13,n);www(17,n);www(19,n); work(n,a);LL all=zuo(n); if (all==-1) { printf("Creation August is a SB!\n"); continue; } LL ans=0; for (LL i=1;i<=nd;i++) { LL x,y; exgcd(all/mo[i],mo[i],x,y); while (x<0) x+=mo[i]; x%=mo[i]; ans=(ans+x*(all/mo[i])%all*yu[i]%all)%all; //ans=(ans+times(times(x,all/mo[i],all),yu[i],all))%all; } for (int i=1;i<=top;i++) if (ans%p1[i]!=p2[i]) assert(0); if (ans==0) ans=all; printf("%I64d\n",ans); } }