ERROR: TLE, MAJIIN VEGETA

I don’t kmow, but I am getting an error TLE in my code, i have tried many times and still the error persists.
I request you to please help me correcting my code.

Here is my code: CodeChef: Practical coding for everyone
Link to question: VEGETA Problem - CodeChef
Problem code: VEGETA

As you are calling sieve for each query. Bring sieve function call out of while loop.