from collections import deque

# initial state
init = frozenset({'M', 'G', 'W', 'C' }), frozenset()

# bad configurations
bad = [frozenset({'W', 'G'}), frozenset({'G', 'C'})]

def next(s):
    bank = 0 if 'M' in s[0] else 1

    for x in s[bank]:
        source = s[bank] - frozenset({'M', x})
        target = s[1 - bank] | frozenset({'M', x})

        if any(b <= source for b in bad):
            continue

        yield (target, source) if bank else (source, target)

Q = deque([init])
V = {init: None}

solved = False

while Q:
  s = Q.popleft()

  if not s[0]:
      solved = True
      break

  for n in next(s):
      if not n in V:
          Q.append(n)
          V[n] = s


def printSolution(s): 
  if s:
      printSolution(V[s])
      print(s)

if solved: printSolution(s)
