TCP: Travelling Chef problem

How is this code correct CodeChef: Practical coding for everyone ?
Or it gave AC due to something wrong with the test cases or anything…