I have run this code against as many test examples I could fine and my answers match the test results.
My code is at http://www.codechef.com/viewsolution/3301834
I have tested for invalid EOF, and all conditions I think surround the problem. Obviously, I am missing something so I am asking for a pointer to what I am missing. Thanks to anyone available to help.