code:
Testcase 1 2 3 showing run error :
@myself.yash.srivastav in the line number 90 of your code pass k%n instead of k to the function.
Hope that helps.
modified
now showing wrong answer in testcase 3
Do the dry run once, there must be a minute error which can be taken care off by a simple dry run.