I tried this question on SPOJ but i am getting a wrong asnwer message.
I Also Added the condition of multiples test cases.
Getting Wrong Answer on Spoj
@ubaidshaikh9999 on spoj first take t as a test case numbers input.
See the input format on spoj. I have submitted your code only with multiple test cases on spoj and it is accepted.
Also if your doubt is cleared, resolve this and rate!