Prateek loves candy

1 test case is showing tle.
where I am going wrong?

Instead of iterating over p for each test case construct an array of primes such that ith index of that array corresponds to i+1 st prime that way you’d be able to solve each query in O(1)

I have done that ,can you please correct the code?

you have not for each test case you are iterating over the p[i], please try to make an prime array.
Corrected code