Combination Lock

Most combination locks consist of a dial with the numbers 0 through n-1 printed on it in clockwise order. The dial can be turned either clockwise or counterclockwise, bringing one of the numbers to the top of the dial (if 0 is at the top of the dial, a turn of 1 in the counterclockwise direction would bring 1 to the top). Each lock has a three number code (x, y, z) and can only be opened after the following series of steps:

  1. The lock dial must first be spun clockwise at least one full rotation, ending with a number x at the top. This can be accomplished with consecutive clockwise rotations.
  2. The lock must be turned counterclockwise until the number y appears at the top for the second time. This can be accomplished with consecutive clockwise turns.
  3. The lock must then be turned clockwise until the number z appears on top, without going more than one full rotation. This can be accomplished with consecutive clockwise turns.
Input is, in order, n, x, y, z, followed by pairs of 'C <number>' or 'CC <number>' followed by a '?'. A pair 'C <number>' means turns the lock clockwise past <number> numbers. A pair 'CC <number>' means turn counterclockwise past that number of numbers. Output is either "Closed" or "Open" indicating the lock is opened by the turns or it remains closed. Example input:
   60 6 1 58 C 114 CC 115 C 3 ?