There is nothing wrong with your code!
Make sure you are entering input in the same format as mentioned in the problem. (a blank line at the beginning of each test case)
Don't translate C++ code to Python 
Avoid this:
t = int(raw_input())
while t:
# code
t -= 1
Instead use:
for t in range(int(raw_input())):
# code
Generally constants are labelled in all CAPS. So if it is a variable, don't name it in all CAPS.