from sys import stdin, stdout

INF = float('inf')

n = input()
inp = stdin.readlines()

cd = [[[], [], []] for i in xrange(n)] # cost, damage

ptr = 0
idx = 0
while ptr < len(inp):
    k = int(inp[ptr]); ptr += 1
    
    for i in xrange(k):
        c, d = map(int, inp[ptr].split()); ptr += 1
        cd[idx][c - 1].append(d)

    idx += 1

# sort
for i in xrange(n):
    for j in xrange(3):
        cd[i][j].sort(reverse = True)

# dp[i][j]: max damage dealt if first i cards dealt, total number of cards dealt mod 10 = j
dp = [[-INF for i in xrange(10)] for j in xrange(n)]

# base case
i = 0

# no card
dp[i][0] = 0

# 1 card
for j in xrange(3):
    if len(cd[i][j]): dp[i][1] = max(dp[i][1], cd[i][j][0])

# 2 cards
if len(cd[i][0]) >= 1 and len(cd[i][1]) >= 1:
    dp[i][2] = cd[i][0][0] + cd[i][1][0]
if len(cd[i][0]) >= 2:
    dp[i][2] = max(dp[i][2], cd[i][0][0] + cd[i][0][1])

# 3 cards
if len(cd[i][0]) >= 3: dp[i][3] = cd[i][0][0] + cd[i][0][1] + cd[i][0][2]

# iterate
for i in xrange(1, n):
    for j in xrange(10):
        # this is an impossible transition state, ignore it
        if dp[i - 1][j] == -INF: continue

        # 1 card with max damage
        dmg1 = 0
        for cost in xrange(3):
            try: dmg1 = max(dmg1, cd[i][cost][0])
            except: dmg1 = dmg1

        assert (dmg1 != 0) # there is atleast 1 card in each turn with min dmg >= 1

        # 2 cards with max damage
        dmg2, dmg3 = -INF, -INF

        if len(cd[i][0]) >= 1 and len(cd[i][1]) >= 1:
            dmg3, dmg2 = sorted([cd[i][0][0], cd[i][1][0]])
        if len(cd[i][0]) >= 2:
            if cd[i][0][0] + cd[i][0][1] > dmg2 + dmg3:
                dmg3, dmg2 = sorted([cd[i][0][0], cd[i][0][1]])

        # 3 cards with max damage
        dmg4, dmg5, dmg6 = -INF, -INF, -INF
        if len(cd[i][0]) >= 3: dmg4, dmg5, dmg6 = cd[i][0][0], cd[i][0][1], cd[i][0][2]

        # draw no cards
        dp[i][j] = max(dp[i][j], dp[i - 1][j])

        # draw 1 card
        if j + 1 < 10:
            dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + dmg1)
        else:
            dp[i][j + 1 - 10] = max(dp[i][j + 1 - 10], dp[i - 1][j] + 2 * dmg1)

        # draw 2 cards
        if j + 2 < 10:
            dp[i][j + 2] = max(dp[i][j + 2], dp[i - 1][j] + dmg2 + dmg3)
        else:
            dp[i][j + 2 - 10] = max(dp[i][j + 2 - 10], dp[i - 1][j] + 2 * dmg2 + dmg3)

        # draw 3 cards
        if j + 3 < 10:
            dp[i][j + 3] = max(dp[i][j + 3], dp[i - 1][j] + dmg4 + dmg5 + dmg6)
        else:
            dp[i][j + 3 - 10] = max(dp[i][j + 3 - 10], dp[i - 1][j] + 2 * dmg4 + dmg5 + dmg6)

# print answer
print max(dp[n - 1])
