prime queries

problem : http://www.spoj.com/problems/PRMQUER/
solution :
http://ideone.com/YwZPRr

I am getting wa,
After tonnes of optimization i was successful in removing TLE
but now WA

Really frustrated plz HELP!!!