import sys

input = lambda: sys.stdin.readline().rstrip()

gears = []

t = int(input())
for _ in range(t):
    gear = list(map(int, list(input())))
    gears.append(gear)


def rotation(num, clockwise):
    gear = gears[num]
    if clockwise == 1:
        gear = gear[7:8] + gear[:7]
    else:
        gear = gear[1:8] + gear[0:1]

    gears[num] = gear


for _ in range(int(input())):
    num, clockwise = map(int, input().split())
    num -= 1

    rotation(num, clockwise)

    # 왼쪽 톱니바퀴 돌리기
    left = num - 1
    left_clockwise = clockwise
    while left >= 0:
        if gears[left][2] == gears[left + 1][6 + left_clockwise]:
            break

        left_clockwise *= -1
        rotation(left, left_clockwise)

        left -= 1

    # 오른쪽 톱니바퀴 돌리기
    right = num + 1
    right_clockwise = clockwise
    while right < t:
        if gears[right][6] == gears[right - 1][2 + right_clockwise]:
            break

        right_clockwise *= -1
        rotation(right, right_clockwise)

        right += 1

result = 0
for i in range(t):
    result += gears[i][0]

print(result)
