How can I shorten this python code? Here is the code which I want to shorten.

n=input()
while n:
 s=raw_input()
 x,r,g,b=(int(x) for x in s.split())
 a=x/r%2
 c=x/g%2
 d=x/b%2
 r=((a*10+c)*10)+d
 if r==0:e="black"
 elif r==100:e="red"
 elif r==1:e="blue"
 elif r==10:e="green"
 elif r==101:e="magenta"
 elif r==11:e="cyan"
 elif r==110:e="yellow"
 else:e="white"
 print(e)
 n-=1


Input:
3

4643 5913 4827 9752
5583 5357 5120 9400
2025 5475 4339 8392


Output:

black
yellow
black
Instead of ((a*10+c)*10)+d we can use ((a*2+c)*2)+d to distinguish the colors.

 r=((a*2+c)*2)+d
 if r==0:e="black"
 elif r==4:e="red"
 elif r==1:e="blue"
 elif r==2:e="green"
 elif r==5:e="magenta"
 elif r==3:e="cyan"
 elif r==6:e="yellow"
 else:e="white"


Ah, but now we're just distinguishing between values from 0 to 7, so we can index into an array instead!

 r=a*4+c*2+d
 e=["black","blue","green","cyan","red","magenta","yellow","white"][r]
 # or even shorter:
 e="black blue green cyan red magenta yellow white".split()[r]


Combining with Uriel's changes we get down to 136 bytes (164 bytes saved).

exec'x,r,g,b=map(int,raw_input().split());print"black blue green cyan red magenta yellow white".split()[x/r%2*4+x/g%2*2+x/b%2];'*input()


Try it online!