How can I shorten this python code?

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

 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"

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

 # 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!

For the repetition use an exec statement,

map(int, for the conversion of string input into numerals,

shorten calculating r with r=a*100+c*10+d, then put the calculations of each variable (a, c, d) instead of the variable,

and for the conditions use a dictionary with a get query.

Finally, mash everything into one line.

Final result (updating):


Bytes saved: 121.