# 무선 충전 - SWEA

무선 충전 - SWEA

dx = [0, -1, 0, 1, 0]
dy = [0, 0, 1, 0, -1]

def power(u):
    p = [0] * A
    for i in range(A):
        if abs(u[0] - ap[i][1] + 1) + abs(u[1] - ap[i][0] + 1) <= ap[i][2]:
            p[i] = ap[i][3]
    return p

def calculate(p1, p2):
    ret = 0
    if A == 1: return max(p1[0], p2[0])
    for i in range(A):
        for j in range(A):
            if i != j:
                ret = max(p1[i] + p2[j], ret)
    return ret

T = int(input())

for test_case in range(1, T + 1):
    M, A = map(int, input().split())
    ma = list(map(int, input().split()))
    mb = list(map(int, input().split()))
    ap = [list(map(int, input().split())) for _ in range(A)]

    ua = [0, 0]
    ub = [9, 9]
    _sum = 0
    _sum += calculate(power(ua), power(ub))

    for i in range(M):
        ua[0] += dx[ma[i]]
        ua[1] += dy[ma[i]]
        ub[0] += dx[mb[i]]
        ub[1] += dy[mb[i]]
        _sum += calculate(power(ua), power(ub))

    print("#" + str(test_case) + " " + str(_sum))