n = int(eval(input()))

flst = []

for _ in range(n):

  flst.append([int(i) for i in input().split()])

plst = []

for _ in range(n):

  plst.append([int(i) for i in input().split()])

m = - 1 << 30

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

  r = 0

  for j in range(n):

    c = 0

    for k in range(10):

      if (i >> k & 1) & flst[j][k]:

        c += 1

    r += plst[j][c]

  m = max(m, r)

print(m)

n = int(eval(input()))

flst = []

for _ in range(n):

  flst.append(int(''.join(input().split()), 2))

plst = []

for _ in range(n):

  plst.append([int(i) for i in input().split()])

m = - 1 << 30

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

  r = 0

  for j in range(n):

    s = i & flst[j]

    c = 0

    while s:

        s &= s - 1

        c += 1

    r += plst[j][c]

  m = max(m, r)

print(m)
n = int(eval(input()))
flst = []
for _ in range(n):
    flst.append([int(i) for i in input().split()])
plst = []
for _ in range(n):
    plst.append([int(i) for i in input().split()])
m = -1 << 30
for i in range(1, 1 << 10):
    r = 0
    for j in range(n):
        c = 0
        for k in range(10):
            if (i >> k & 1) & flst[j][k]:
                c += 1
        r += plst[j][c]
    m = max(m, r)
print(m)

n = int(eval(input()))
flst = []
for _ in range(n):
    flst.append(int("".join(input().split()), 2))
plst = []
for _ in range(n):
    plst.append([int(i) for i in input().split()])
m = -1 << 30
for i in range(1, 1 << 10):
    r = 0
    for j in range(n):
        s = i & flst[j]
        c = 0
        while s:
            s &= s - 1
            c += 1
        r += plst[j][c]
    m = max(m, r)
print(m)

-    flst.append([int(i) for i in input().split()]) +    flst.append(int("".join(input().split()), 2)) +        s = i & flst[j] -        for k in range(10): -            if (i >> k & 1) & flst[j][k]: -                c += 1 +        while s: +            s &= s - 1 +            c += 1