Why some test cases are showing WA?

This is my solution to the problem PSHTRG. Can anyone please help me to find the bug in the code.

Note: I have followed this editorial.