from __future__ import division, print_function

DEBUG = 0

import os, sys
from atexit import register
from io import BytesIO
import itertools

if sys.version_info[0] < 3:
    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip

if DEBUG:
    debug_print = print
else:
    sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
    sys.stdout = BytesIO()
    register(lambda: os.write(1, sys.stdout.getvalue()))

    input = lambda: sys.stdin.readline().rstrip('\r\n')
    debug_print = lambda *x, **y: None


def input_as_list():
    return list(map(int, input().split()))

def array_of(f, *dim):
    return [array_of(f, *dim[1:]) for _ in range(dim[0])] if dim else f()

def main():
    n = int(input())
    cards = []

    for _ in range(n):
        this = []
        k = int(input())
        for _ in range(k):
            c = input_as_list()
            this.append(c)
        cards.append(this)

    dp = array_of(lambda: -1, 10)
    dp[0] = 0
    for cl in cards:
        newdp = list(dp)
        max11, max12, max13, max2, max3 = -1, -1, -1, -1, -1

        for c, d in cl:
            if c == 1:
                if d > max11:
                    max11, max12, max13 = d, max11, max12
                elif d > max12:
                    max12, max13 = d, max12
                elif d > max13:
                    max13 = d
            elif c == 2:
                max2 = max(max2, d)
            elif c == 3:
                max3 = max(max3, d)

        strategies = [[max11], [max11, max12], [max12, max11],
                      [max11, max12, max13], [max12, max11, max13],
                      [max13, max12, max11], [max2], [max2, max11], [max11, max2],
                      [max3]]

        for s in strategies:
            if -1 in s: continue
            cnt = len(s)

            for i in range(10):
                if dp[i] == -1: continue
                value = sum(s)
                if i + cnt >= 10:
                    value += s[9-i]
                idx = (i + cnt) % 10
                newdp[idx] = max(newdp[idx], dp[i]+value)

        dp = newdp
        debug_print(dp)

    print(max(dp))


main()