Forgetpw wa

Can someone please point out where I am making a mistake. I have spent hours on this ad-hoc question, and am clueless as to where I am going wrong. My code does work fine for the tricky cases given in the editorial.

http://www.codechef.com/viewsolution/4113943