Doubt in CNOTE

I submitted the solution for CNOTE and found that the solution was accepted when I tried to print the output outside the loop. Printing and breaking the loop works just as fine for one of the condition. What am I missing here ?

AC : https://www.codechef.com/viewsolution/25153642
WA : https://www.codechef.com/viewsolution/25153541

Ps : I’m a newbie to solving CP. Any improvements or suggestions for my code would be appreciated.