n = int(eval(input()))

f=[]

for i in range(n):

    tmp = list(map(int,input().split()))

    f.append(tmp)

p=[]

for i in range(n):

    tmp = list(map(int,input().split()))

    p.append(tmp)



from itertools import *

first=True

ans=-10**12

for tmp in list(product([0,1], repeat=10)):

    if first:

        first=False

        continue



    ona=[]

    for mise in f:

        cnt=0

        for j in range(10):

            cnt+= tmp[j] & mise[j]

        ona.append(cnt)



    sm=0

    for i in range(len(p)):

        sm+=p[i][ona[i]]

    if ans < sm:

        ans=sm

print(ans)
n = int(eval(input()))

f=[]

for i in range(n):

    tmp = list(map(int,input().split()))

    f.append(tmp)

p=[]

for i in range(n):

    tmp = list(map(int,input().split()))

    p.append(tmp)



from itertools import *

first=True

ans=-10**12

for tmp in list(product([0,1], repeat=10)):

    if first:

        first=False

        continue

    sm=0

    for i in range(n):

        cnt=0

        for j in range(10):

            cnt+= tmp[j] & f[i][j]

        sm+=p[i][cnt]

    if ans < sm:

        ans=sm

print(ans)
n = int(eval(input()))
f = []
for i in range(n):
    tmp = list(map(int, input().split()))
    f.append(tmp)
p = []
for i in range(n):
    tmp = list(map(int, input().split()))
    p.append(tmp)
from itertools import *

first = True
ans = -(10**12)
for tmp in list(product([0, 1], repeat=10)):
    if first:
        first = False
        continue
    ona = []
    for mise in f:
        cnt = 0
        for j in range(10):
            cnt += tmp[j] & mise[j]
        ona.append(cnt)
    sm = 0
    for i in range(len(p)):
        sm += p[i][ona[i]]
    if ans < sm:
        ans = sm
print(ans)

n = int(eval(input()))
f = []
for i in range(n):
    tmp = list(map(int, input().split()))
    f.append(tmp)
p = []
for i in range(n):
    tmp = list(map(int, input().split()))
    p.append(tmp)
from itertools import *

first = True
ans = -(10**12)
for tmp in list(product([0, 1], repeat=10)):
    if first:
        first = False
        continue
    sm = 0
    for i in range(n):
        cnt = 0
        for j in range(10):
            cnt += tmp[j] & f[i][j]
        sm += p[i][cnt]
    if ans < sm:
        ans = sm
print(ans)

-    ona = [] -    for mise in f: +    sm = 0 +    for i in range(n): -            cnt += tmp[j] & mise[j] -        ona.append(cnt) -    sm = 0 -    for i in range(len(p)): -        sm += p[i][ona[i]] +            cnt += tmp[j] & f[i][j] +        sm += p[i][cnt]