Help to resolve WA in spoj TEMPLEQ problem.

I have been solving TEMPLEQ problem from spoj since very long, but I am getting WA. This is link to my code. anyone please help.

@vivek_1998299

Can you please help me with this, I have tried a lot.

Thanks in advance.