N = int(input())
DP = [0] * 10
for _ in range(N):
    K = int(input())
    X = [[] for aa in range(3)]
    for __ in range(K):
        c, d = map(int, input().split())
        X[c-1].append(d)
    X[0] = sorted(X[0])[::-1]
    X[1] = sorted(X[1])[::-1]
    X[2] = sorted(X[2])[::-1]
    
    NE = [x for x in DP]
    a1 = max(X[0][:1]+X[1][:1]+X[2][:1])
    NE[0] = max(NE[0], DP[9] + a1*2)
    for i in range(9):
        NE[i+1] = max(NE[i+1], DP[i] + a1)
    
    if len(X[0]) >= 2:
        NE[0] = max(NE[0], DP[8] + X[0][0] * 2 + X[0][1])
        NE[1] = max(NE[1], DP[9] + X[0][0] * 2 + X[0][1])
        for i in range(2, 10):
            NE[i] = max(NE[i], DP[i-2] + X[0][0] + X[0][1])
            
    if len(X[0]) >= 1 and len(X[1]) >= 1:
        a2 = X[0][0] + X[1][0]
        ma = X[0][0] + X[1][0] + max(X[0][0], X[1][0])
        NE[0] = max(NE[0], DP[8] + ma)
        NE[1] = max(NE[1], DP[9] + ma)
        for i in range(2, 10):
            NE[i] = max(NE[i], DP[i-2] + X[0][0] + X[1][0])
    
    if len(X[0]) >= 3:
        a3 = sum(X[0][:3])
        ma = X[0][0] * 2 + X[0][1] + X[0][2]
        
        NE[0] = max(NE[0], DP[7] + ma)
        NE[1] = max(NE[1], DP[8] + ma)
        NE[2] = max(NE[2], DP[9] + ma)
        for i in range(3, 10):
            NE[i] = max(NE[i], DP[i-3] + a3)
    
    DP = NE
print(max(DP))
