import sys,math
from fractions import gcd
from bisect import bisect_left, bisect
from collections import defaultdict
from io import BytesIO
sys.stdin = BytesIO(sys.stdin.read())
input = lambda: sys.stdin.readline().rstrip('\r\n')
n = int(input())

#s = list(input())
#l = len(s)
#arr = [int(_) for _ in input().split()]
d = [0] * 10
for step in range(n):
    k = int(input())
    b3,b2,b11,b12,b13 = 0,0,0,0,0
    for i in range(k):
        c,s = [int(_) for _ in input().split()]
        if c == 3 and s > b3:
            b3 = s
        elif c == 2 and s > b2:
            b2 = s
        elif c == 1:
            if s > b11:
                b11,b12,b13 = s,b11,b12
            elif s > b12:
                b12,b13 = s,b12
            elif s > b13:
                b13 = s
    newd = d[:]
    for i in range(10):
        if i == 0 or d[i]:
            nz = 0
            if b11 > 0:
                nz += 1
            if b12 > 0:
                nz += 1
            if b13 > 0:
                nz += 1
            if nz:
                if nz + i >= 10:
                    op = 1
                else:
                    op = 0
                newd[(i+nz)%10] = max(newd[(i+nz)%10],d[i] + b11 + b12 + b13 + op * max(b11,b12,b13))
            if b2 > 0:
                if nz > 0:
                    if 2 + i >= 10:
                        op = 1
                    else:
                        op = 0
                    newd[(i+2)%10] = max(newd[(i+2)%10],d[i] + b2 + max(b11,b12,b13) + op * max(b2,b11,b12,b13))
                else:
                    if 1 + i >= 10:
                        op = 1
                    else:
                        op = 0
                    newd[(i+1)%10] = max(newd[(i+1)%10],d[i] + b2 + op * b2)
            if b3 > 0:
                if i + 1 >= 10:
                    op = 1
                else:
                    op = 0
                newd[(i+1)%10] = max(newd[(i+1)%10],d[i] + b3 + op * b3)
    d = newd[:]
    #print(step,d)
print(max(d))
                