please help me in finding the reason for this code showing WA

#include<stdio.h>

int main()

{

int t;

long long int divisor,a,dividend,lcm,n,i,length,s;

scanf("%d",&t);

while(t)

{

```
scanf("%lld",&n);
long long int visit[n+1],arr[n+1];
for(i=1;i<=n;i++)
{
scanf("%lld",&arr[i]);
visit[i]=0;
}
lcm=1;
for(i=1;i<=n;i++)
{
if(visit[i]==0)
{
length=1;
visit[i]=1;
s=arr[i];
while(i!=s)
{
visit[s]=1;
length++;
s=arr[s];
}
if(length<lcm)
{
a=lcm;
lcm=length;
length=a;
}
dividend=length;
divisor=lcm;
do
{
a=dividend%divisor;
dividend=divisor;
divisor=a;
}while(divisor!=0);
lcm=(length*lcm)/dividend;
}
}
lcm%=1000000007;
printf("%lld\n",lcm);
t--;
}
return 0;
```

}