Robot#759

Robot

1 second
64 megabytes
Hard

A factory workshop is a rectangle measuring by meters (1 \leq M, N \leq 30). Engineer Petro has created a robot that is able to move around the workshop and perform useful tasks. The robot moves only on tiles of 1 \times 1 meters that form the floor of the workshop, and can move only parallel to the coordinate axes.

The robot has state registers: , , , and . Each register can be either or . Some tiles contain radio triggers, which change the state of certain registers. Also, some other tiles may have radio beacons, which make the robot turn degrees left or right, depending on the truth of a logical formula assigned to the beacon. If the formula evaluates to true, the robot turns right; otherwise, it turns left.

Special services became interested in Petro's robot and decided to test its suitability for work in extreme conditions—such as under radiation, underwater, in volcanic craters, on other planets, and in many other places. For the tests, all the equipment was removed from the workshop, and a certain number of radio beacons and triggers were placed. The robot starts from a certain empty tile X_0, Y_0 and an initial orientation A_0 (, or degrees, measured clockwise from up). At the start, all register values are .

The robot's battery is sufficient for (0 < K \leq 10^9) moves to an adjacent tile. After that, it stops. There is also a possibility that Petro may have placed the triggers and beacons incorrectly, so the robot might crash into a workshop wall at some step. You need to determine whether the robot will survive the tests and, if so, on which tile it will stop.

The coordinate axes are directed from the top-left corner—point —to the right and downward. is the horizontal size of the workshop, is the vertical size. The number of triggers is (not more than ), and the number of radio beacons is (not more than ).

Input

The first line of the input contains numbers: M, N, P, Q, K, X_0, Y_0, A_0. The next lines describe the triggers in the format "", where is the register name. The next lines describe the radio beacons in the format "", where is a logical function with variables , with a maximum length of characters, given as a correct formula, such that:

  • , , , , , are valid formulas.

  • If is a valid formula, "" and "NOT F" are also valid formulas.

  • If and are valid formulas, so are "F AND G", "F OR G", and "F XOR G". The NOT operation has the highest precedence; the other operations have equal precedence and are evaluated left to right—that is, A\ \text{AND}\ \text{NOT}\ B\ \text{OR}\ C\ \text{XOR}\ D is equivalent to (((A\ \text{AND}\ (\text{NOT}\ B))\ \text{OR}\ C)\ \text{XOR}\ D).

  • Register letter case does not matter.

  • A valid formula contains no extra spaces.

Output

If the robot completes all the tests successfully, output the coordinates of the tile where it stops. If the experiment fails, print a single number "-1".

Examples

Example 1

Loading
Loading

Note

In the illustration, the robot moves along a "figure eight" with a total length of meters. Therefore, passing 42n+1 meters is equivalent to passing meter, so the robot will stop at tile "3 5".