Test cases exceed the given data

If the given number of test cases exceeds the provided data the while loop reruns using the same values as that of the previous iteration. How to prevent it?

I don’t fully understand your question, but if you wanna prevent your variables carrying on calculations with old test data then:

re-initialise variables to default values like 0 if you’re storing sum, and 1 in case of product, and writing things like map.clear() or set.clear() etc, clean all traces of computation from the previous test case.

Suppose I set the number of test cases to 3(let us assume ‘t’) and use the while(t–) way to iterate but provide only 2 set of following inputs the loop runs once more using the last set of values as the inputs causing flawed output.

Test cases are always correct, if you are sure the input given is wrong, you can report it to codechef, by emailing them.
If someone else has got the correct answer for the question you are talking about, you too can get AC on it, you’re just going wrong somewhere.

And as you say in the question, the while loop will keep waiting if there aren’t enough cases in input, and you will get TLE, the while loop will not run on previous test case.

You don’t set test cases yourself. You take the ‘N’ number of testcases as input and then run the loop for each testcase ‘N’ times.