I'm getting tle in the first case

What could be the corner case for which i’m getting a tle

instead of iterating over seive for each test case, iterate it once and make an different arrray such that ith index of that array corresponds to ith prime number and use it to answer each query in O(1)
Changed Code