my solution :-
giving right ans in all test cases as much as i can think
it is quite difficult to look someone’s code for the possible error .
But i request you to have a look on the editorial for the given problem and check whether you have taken care of all the things mentioned in the editorial and even this does not work then look on someone’s other code.
may it help you.
if still have doubts feel free to ask
if you like this answer ,feel free to cast votes.