Help me in solving LB02A problem

My issue

My code

# Step 1: Read and understand the problem statement and sample test cases

# Click on 'Next' once you are ready to proceed.
for t in range(tc):

Learning course: Python for problem solving - 1
Problem Link: CodeChef: Practical coding for everyone

Please click next, the logic is explained ahead.