NUKES..Wrong Answer

Can someone please tell me where i am going wrong? Two of my test cases show a wrong answer (test 3 and test 8).Thanks in advance.
Following is the link of my code: http://ideone.com/ztgorj

I had written another code but that shows a TLE error for test case 6.
Following is the link to my code : http://www.codechef.com/viewsolution/3116951

//