from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

from itertools import accumulate
from functools import lru_cache

M = mod = 10 ** 9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
 
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]

n = val()

l = []
for i in range(n):
    
    curr = []
    for i in range(val()):
        curr.append(li())
    l.append(curr[:])

# for i in l:print(*i)

for i in range(n):
    
    arr1 = sorted([j[1] for j in l[i] if j[0] == 1], reverse = 1)
    arr2 = sorted([j[1] for j in l[i] if j[0] == 2], reverse = 1)
    arr3 = sorted([j[1] for j in l[i] if j[0] == 3], reverse = 1)


    ones = [-float('inf') if not arr1 else arr1[0]]


    twos = [-float('inf')] * 2
    if len(arr1) > 1:
        twos[-1] = [arr1[0], arr1[1]]
    twos[0] = -float('inf') if not arr2 else arr2[0]



    threes = [-float('inf')] * 3

    if len(arr1) > 2:
        threes[-1] = [arr1[0], arr1[1], arr1[2]]
    
    if len(arr2) and len(arr1):
        threes[-2] = sorted([arr2[0], arr1[0]])
    
    if len(arr3):
        threes[0] = [arr3[0]]

    l[i] = [ones[:], twos[:], threes[:]]
    

# for i in l:print(*i)

@lru_cache(None)
def dp(i = 0, turn = 0):
    if i == n:return 0
    
    ans = dp(i + 1, turn)
    
    # print(l[i], 'SEP' , l[i][2], dp(i + 1, (turn + 1) % 10))
    if l[i][0][0] != -float('inf'):
        ans = max(ans, dp(i + 1, (turn + 1) % 10) + (l[i][0][0]) * (2 if turn == 9 else 1))
    if l[i][1][0] != -float('inf'):
        ans = max(ans, dp(i + 1, (turn + 1) % 10) + (l[i][1][0]) * (2 if turn == 9 else 1))
    if l[i][2][0] != -float('inf'):
        ans = max(ans, dp(i + 1, (turn + 1) % 10) + (l[i][2][0][0]) * (2 if turn == 9 else 1))
    

    if l[i][1][1] != -float('inf'):
        a = l[i][1][1][0] * 2 + l[i][1][1][1]
        b = a - l[i][1][1][0]
        ans = max(ans, dp(i + 1, (turn + 2) % 10) + (a if 8 <= turn <= 9 else b))
    if l[i][2][1] != -float('inf'):
        a = l[i][2][1][0] * 2 + l[i][2][1][1]
        b = a - l[i][2][1][0]
        ans = max(ans, dp(i + 1, (turn + 2) % 10) + (a if 8 <= turn <= 9 else b))


    if l[i][2][2] != -float('inf'):
        b = sum(l[i][2][2])
        a = b + l[i][2][2][0]
        ans = max(ans, dp(i + 1, (turn + 3) % 10) + (a if 7 <= turn <= 9 else b))

    return ans
print(dp())