import sys
import time
sys.stdin = open(r"섹션 3/8. 곳감/in2.txt", "r")
N = int(input())
arr = [list(map(int, input().split())) for _ in range(N)]
M = int(input())
# 1) array slicing
for _ in range(M):
rownum, direction, step = map(int, input().split()) # direction: 0 = left, 1 = right
if direction == 0:
arr[rownum-1] = arr[rownum-1][step:] + arr[rownum-1][:step]
else:
arr[rownum-1] = arr[rownum-1][N-step:] + arr[rownum-1][:N-step]
# 2) using pop() & insert()
# for i in range(M):
# h, t, k = map(int, input().split())
# if(t == 0):
# for _ in range(k):
# arr[h-1].append(arr[h-1].pop(0))
# else:
# for _ in range(k):
# arr[h-1].insert(0, arr[h-1].pop())
# sum of sandglass
p1 = 0
p2 = N
sums = 0
for i in range(N):
sums += sum(arr[i][p1:p2])
if i < N//2:
p1 += 1
p2 -= 1
else:
p1 -= 1
p2 += 1
print(sums)