T = int(input())
dir_dict = {
0: [0, 0],
1: [0, -1],
2: [1, 0],
3: [0, 1],
4: [-1, 0],
}
for tc in range(1, 1+T):
M, A = map(int, input().split())
a_directions = list(map(int, input().split()))
b_directions = list(map(int, input().split()))
battery_chargers = []
for _ in range(A):
battery_chargers.append(list(map(int, input().split())))
a_position = [1, 1]
b_position = [10, 10]
answer = 0
a_max = 0
b_max = 0
for charger in battery_chargers:
if abs(a_position[0] - charger[0]) + abs(a_position[1] - charger[1]) <= charger[2]:
if a_max < charger[3]:
a_max = charger[3]
if abs(b_position[0] - charger[0]) + abs(b_position[1] - charger[1]) <= charger[2]:
if b_max < charger[3]:
b_max = charger[3]
answer += a_max + b_max
for turn in range(M):
a_dir = a_directions[turn]
a_position[0] += dir_dict[a_dir][0]
a_position[1] += dir_dict[a_dir][1]
b_dir = b_directions[turn]
b_position[0] += dir_dict[b_dir][0]
b_position[1] += dir_dict[b_dir][1]
a_can_charging = []
b_can_charging = []
for charger in battery_chargers:
if abs(a_position[0] - charger[0]) + abs(a_position[1] - charger[1]) <= charger[2]:
a_can_charging.append(charger)
if abs(b_position[0] - charger[0]) + abs(b_position[1] - charger[1]) <= charger[2]:
b_can_charging.append(charger)
max_sum = 0
len_a_can_charging = len(a_can_charging)
len_b_can_charging = len(b_can_charging)
if not len_a_can_charging and len_b_can_charging:
for b_candidate in b_can_charging:
if max_sum < b_candidate[3]:
max_sum = b_candidate[3]
elif len_a_can_charging and not len_b_can_charging:
for a_candidate in a_can_charging:
if max_sum < a_candidate[3]:
max_sum = a_candidate[3]
elif len_a_can_charging and len_b_can_charging:
for a_candidate in a_can_charging:
for b_candidate in b_can_charging:
if a_candidate == b_candidate:
if max_sum < a_candidate[3]:
max_sum = a_candidate[3]
else:
if max_sum < a_candidate[3] + b_candidate[3]:
max_sum = a_candidate[3] + b_candidate[3]
answer += max_sum
print(f'#{tc} {answer}')