Making a Meal

I tried solving this question.
Will anyone please tell me why my first submission is giving WA when I am using a map to perform the same task as in submission 2 which got an AC.

WA Code:- CodeChef: Practical coding for everyone

AC Code:- CodeChef: Practical coding for everyone

in the WA you are not reading t (number of testcases).
And don’t forget to manage the case the map is empty