from itertools import permutations
import sys
inf = float("inf")
def input(): return sys.stdin.readline()


def main():
    n = int(input())
    cards = [[] for i in range(n)]
    for i in range(n):
        k = int(input())
        group = [[] for i in range(3)]
        for j in range(k):
            cost, damage = map(int, input().split())
            group[cost-1].append(damage)

        for j in range(3):
            group[j].sort(reverse=True)
            if j == 0:
                for p in range(min(3, len(group[j]))):
                    cards[i].append((j+1, group[j][p]))
            else:
                for p in range(min(1, len(group[j]))):
                    cards[i].append((j+1, group[j][p]))

    dp = [[-inf]*(10) for i in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        L = len(cards[i])
        subdp = [[-inf]*(2) for _ in range(4)]
        subdp[0][0] = 0
        for v in permutations(cards[i]):
            total_cost = 0
            total_add = 0
            for j in range(L):
                cst, dmg = v[j]
                total_cost += cst
                total_add += dmg
                if total_cost > 3:
                    break
                subdp[j+1][0] = max(subdp[j+1][0], total_add)
                subdp[j+1][1] = max(subdp[j+1][1], total_add + dmg)
        for j in range(10):
            for k in range(4):
                mod = (j+k) % 10
                if mod > 0:
                    dp[i+1][mod] = max(dp[i+1][mod], dp[i][j]+subdp[k][0])
                else:
                    dp[i+1][mod] = max(dp[i+1][mod], dp[i][j]+subdp[k][1])

    ans = max(dp[n][mod] for mod in range(10))
    print(ans)
    return


if __name__ == "__main__":
    main()

