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)

@lru_cache(None)
def dp(i = 0, j = 0, turn = 0, currcost = 0):
    if i == n:return 0
    if j == len(l[i]):return dp(i + 1, 0, turn, 0)
    ans = 0
    if l[i][j][0] + currcost < 4:
        ans = dp(i, j + 1, (turn + 1) % 10, currcost + l[i][j][0]) + l[i][j][1] * (2 if turn == 9 else 1)
    ans = max(ans, dp(i, j + 1, turn, currcost))
    return ans
print(dp())