Under construction

Still being built. Things may change

Gears

Time limit2sMemory limit512 MB

Summary
Simulate K gear turns; each turn makes a named gear rotate and may propagate to neighbors when the touching poles differ, then print a weighted score from the four top teeth.
Level

Medium4 of 10

Topics
Implementation, Simulation, Array
Solved
No attempts yet

Problem

Four gears, each with 8 teeth, are placed in a row as in the picture below. Every tooth is either an N pole or an S pole. The leftmost gear is gear 1, and the gears to its right are gear 2, gear 3, and gear 4.

Four gears placed in a row

You now turn the gears K times. One turn moves a gear by exactly one tooth, either clockwise or counterclockwise.

A gear turned one tooth clockwise

A gear turned one tooth counterclockwise

Each turn names the gear to turn and the direction. For two neighboring gears, the touching teeth are the 3 o'clock tooth of the left gear and the 9 o'clock tooth of the right gear. When gear A turns and the two touching teeth of gear A and its neighbor B have different poles, B turns in the direction opposite to A. If the two poles are the same, B does not turn, and a gear beyond a gear that did not turn does not turn either. The poles are compared in the state that holds right before this turn starts.

Look at the state below.

The state before gear 3 is turned

The touching parts are circled with a green dotted line. If gear 3 turns counterclockwise here, gear 4 turns clockwise. Gear 2 does not turn, because the teeth it shares with gear 3 are both S poles, and gear 1 does not turn because gear 2 did not turn. The result is the picture below.

The state after gear 3 is turned

If gear 1 then turns clockwise, gear 2 turns counterclockwise, and because gear 2 turns, gear 3 turns clockwise at the same time. Gear 4 does not turn, because the teeth it shares with gear 3 have the same pole. The result is the picture below.

The state after gear 1 is turned

Given the initial state of the gears and the list of turns, write a program that computes the total score after all turns are done.

Input

The first four lines give the state of gear 1, gear 2, gear 3, and gear 4, one gear per line. Each state is a string of 8 characters, each 0 or 1, listed from the 12 o'clock tooth in clockwise order. An N pole is 0 and an S pole is 1.

The fifth line has the number of turns KK (1≤K≤1001 \le K \le 100).

Each of the next KK lines describes one turn, in order. A line holds two integers. The first is the number xx of the gear to turn (1≤x≤41 \le x \le 4), and the second is the direction. A direction of 1 means clockwise, and -1 means counterclockwise.

Output

Print the total score of the four gears after all KK turns, on one line. The score is computed like this.

  • Gear 1 scores 0 if its 12 o'clock tooth is an N pole, and 1 if it is an S pole
  • Gear 2 scores 0 if its 12 o'clock tooth is an N pole, and 2 if it is an S pole
  • Gear 3 scores 0 if its 12 o'clock tooth is an N pole, and 4 if it is an S pole
  • Gear 4 scores 0 if its 12 o'clock tooth is an N pole, and 8 if it is an S pole

Examples4

  1. Example 1

    Input
    10101111
    01111101
    11001110
    00000010
    2
    3 -1
    1 1
    
    Expected output
    7
    
  2. Example 2

    Input
    11111111
    11111111
    11111111
    11111111
    3
    1 1
    2 1
    3 1
    
    Expected output
    15
    
  3. Example 3

    Input
    10001011
    10000011
    01011011
    00111101
    5
    1 1
    2 1
    3 1
    4 1
    1 -1
    
    Expected output
    6
    
  4. Example 4

    Input
    10010011
    01010011
    11100011
    01010101
    8
    1 1
    2 1
    3 1
    4 1
    1 -1
    2 -1
    3 -1
    4 -1
    
    Expected output
    5