I am unable to understand my mistake in CARVANS …please help
Your code is completely right, you are getting wrong answer because you are printing some extra lines.
You don’t have to print the value of f inside the loop, you have to print value of variable f just once outside the r loop for each test case.
I just commented that line of your code and submitted, it got accepted.
Here is the submission link http://www.codechef.com/submit/complete/288843-7424--54d28db6cd86f.