N = int(eval(input()))

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

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



ans = -float('inf')

for i in range(1,2**10):

    tmp = 0

    for n in range(N):

        c = 0

        for b in range(10):

            if ((i >> b)&1) & isopen[n][b]:

                c += 1

        tmp += gain[n][c]

    ans = max(ans, tmp)

print(ans)

N = int(eval(input()))

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

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



ans = -float('inf')

for b in range(1,2**10):

    tmp = 0

    for i in range(N):

        c = 0

        for k in range(10):

            if b&(1<<k) and F[i][k]:

                c += 1

        tmp += P[i][c]

    ans = max(ans,tmp)

print(ans)

N = int(eval(input()))
isopen = [list(map(int, input().split())) for i in range(N)]
gain = [list(map(int, input().split())) for i in range(N)]
ans = -float("inf")
for i in range(1, 2**10):
    tmp = 0
    for n in range(N):
        c = 0
        for b in range(10):
            if ((i >> b) & 1) & isopen[n][b]:
                c += 1
        tmp += gain[n][c]
    ans = max(ans, tmp)
print(ans)

N = int(eval(input()))
F = [list(map(int, input().split())) for i in range(N)]
P = [list(map(int, input().split())) for i in range(N)]
ans = -float("inf")
for b in range(1, 2**10):
    tmp = 0
    for i in range(N):
        c = 0
        for k in range(10):
            if b & (1 << k) and F[i][k]:
                c += 1
        tmp += P[i][c]
    ans = max(ans, tmp)
print(ans)

-isopen = [list(map(int, input().split())) for i in range(N)] -gain = [list(map(int, input().split())) for i in range(N)] +F = [list(map(int, input().split())) for i in range(N)] +P = [list(map(int, input().split())) for i in range(N)] -for i in range(1, 2**10): +for b in range(1, 2**10): -    for n in range(N): +    for i in range(N): -        for b in range(10): -            if ((i >> b) & 1) & isopen[n][b]: +        for k in range(10): +            if b & (1 << k) and F[i][k]: -        tmp += gain[n][c] +        tmp += P[i][c]