I was submitting solution for this. I am successsfully able to do only if number of test cases are 1. I tried inserting a loop for more test cases but in his case output is not printed. pls guide in right direction.

According to your last submitted solution, you have not initialized p = 0 and check while(p == 0), so control will never enter while loop and nothing will be printed.
So, initialize p = 0 in main and also before while as for each iteration p = 0 and other variables such as k (right after you accept the array elements from input).
PS:- Not sure, if the solution will result in AC; improve logic a bit.

Link to AC solution:
Hope this helps:)

The last submitted code is working properly of test case I consider is 1 for more than one I was not able to implement loop so I have removed.