n = int(eval(input()))

f = [input().replace(" ", '') for _ in range(n)]

p = [list(map(int, input().split())) for _ in range(n)]



outcome_list = []

for i in range(1, 1 << 10):

    outcome = 0

    for j, x in enumerate(f):

        both = "{:010b}".format(i & int(x, 2))

        n_store = sum(map(int, list(both)))

        outcome += p[j][n_store]

    outcome_list.append(outcome)

print((max(outcome_list)))
N = int(eval(input()))

F = [int(input().replace(" ", ''), 2) for _ in range(N)]

P = [list(map(int, input().split())) for _ in range(N)]



outcome_list = []

for i in range(1, 1 << 10):

    outcome = 0

    for f, p in zip(F, P):

        n_store = "{:010b}".format(i & f).count("1")

        outcome += p[n_store]

    outcome_list.append(outcome)

print((max(outcome_list)))
n = int(eval(input()))
f = [input().replace(" ", "") for _ in range(n)]
p = [list(map(int, input().split())) for _ in range(n)]
outcome_list = []
for i in range(1, 1 << 10):
    outcome = 0
    for j, x in enumerate(f):
        both = "{:010b}".format(i & int(x, 2))
        n_store = sum(map(int, list(both)))
        outcome += p[j][n_store]
    outcome_list.append(outcome)
print((max(outcome_list)))

N = int(eval(input()))
F = [int(input().replace(" ", ""), 2) for _ in range(N)]
P = [list(map(int, input().split())) for _ in range(N)]
outcome_list = []
for i in range(1, 1 << 10):
    outcome = 0
    for f, p in zip(F, P):
        n_store = "{:010b}".format(i & f).count("1")
        outcome += p[n_store]
    outcome_list.append(outcome)
print((max(outcome_list)))

-n = int(eval(input())) -f = [input().replace(" ", "") for _ in range(n)] -p = [list(map(int, input().split())) for _ in range(n)] +N = int(eval(input())) +F = [int(input().replace(" ", ""), 2) for _ in range(N)] +P = [list(map(int, input().split())) for _ in range(N)] -    for j, x in enumerate(f): -        both = "{:010b}".format(i & int(x, 2)) -        n_store = sum(map(int, list(both))) -        outcome += p[j][n_store] +    for f, p in zip(F, P): +        n_store = "{:010b}".format(i & f).count("1") +        outcome += p[n_store]