TLE for some test case.How to modify my code?

you dont have to run the seive each time, just make an array in which ith index corresponds to ith prime and use it to answer all the queries in O(1)
Corrected Code

Got it .Thank You !!