Taking inspiration from the above solutions, here's another very similar way for straightforward algebra lovers:
We want the time taken for Gear Q to make 6 more revolutions than gear P:
Distance covered by P = rxt = 40rev/min x T = 40T
Distance covered by Q = rxt = 10 rev/min x T = 10T
Therefore, Q's distance = 6 + P's distance.
10T = 40T + 6
Solve for T: 30T=6, or T=1/5 minutes.
Convert to seconds: T= 1/5x60 = 12 seconds.