Under construction

Still being built. Things may change

Once Around the Lock

Time limit1sMemory limit128 MB

Summary
Given a circular dial and a code, decide whether a sequence of clockwise and counterclockwise turns unlocks the lock.
Level

Medium5 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

A circular combination lock has a dial with the numbers 00 through n−1n-1 printed on it in clockwise order. Turning the dial brings one of the numbers to the top. If 00 is at the top, turning the dial counterclockwise by 11 brings 11 to the top, and turning it clockwise by 11 brings n−1n-1 to the top.

Each lock has a three-number code (x,y,z)(x, y, z) and can be opened only by performing the following steps in order:

  1. Spin the dial clockwise through at least one full rotation, ending with xx at the top, with no counterclockwise turn in between (this may take several consecutive clockwise turns).
  2. Turn the dial counterclockwise until yy reaches the top for the second time, with no clockwise turn in between (this may take several consecutive counterclockwise turns).
  3. Turn the dial clockwise until zz reaches the top, without going more than one full rotation and with no counterclockwise turn in between (this may take several consecutive clockwise turns).

Any turn made after step 3 closes the lock again.

You are given a lock and a sequence of turns. Before the first turn the lock is closed and the dial has been set with 00 at the top. Determine whether the lock is open after the last turn.

Input

The input contains several test cases.

The first line of each test case has four integers nn xx yy zz: the number of digits on the dial and the three-number code. The values xx, yy, and zz are all different, and n≤1000n \le 1000.

The following line or lines list the turns, each written as d s, where d is C for clockwise or CC for counterclockwise, and s>0s > 0 is how many numbers pass the top during that turn. For example, with n=50n = 50 and 44 currently at the top, CC 6 brings 1010 to the top while C 6 brings 4848 to the top. The list of turns may span several lines and ends with a ?.

A line containing a single 0 follows the last test case.

Output

For each test case, print one line: Case k: Open if the lock is open after the last turn, or Case k: Closed otherwise, where kk is the test case number starting from 11.

Examples2

  1. Example 1

    Input
    60 6 1 58
    C 114 CC 115 C 3 ?
    60 6 1 58
    C 54 CC 115 C 3 ?
    60 6 1 58
    C 54 C 60 CC 115 C 3 ?
    0
    
    Expected output
    Case 1: Open
    Case 2: Closed
    Case 3: Open
    
  2. Example 2

    Input
    10 3 7 1
    C 17 CC 14 C 6 ?
    0
    
    Expected output
    Case 1: Open