import sys
qu = int(input())
data = []
for q in range(qu):
    num = int(sys.stdin.readline())
    max2 = 0
    max3 = 0
    max1 = [0]*4
    #max1.append(0)
    for i in range(num):
        d = [int(i) for i in input().split()]
        if d[0] == 1:
            for j in range(2,-1,-1):
                if d[1] > max1[j]:
                    max1[j+1] = max1[j]
                    max1[j] = d[1]
        elif d[0] == 2:
            if d[1] > max2:
                max2 = d[1]
        else:
            if d[1] > max3:
                max3 = d[1]
    data.append(max1[:-1] + [max2, max3])


poss = [(1,(0,)), (1,(1,)), (1,(2,)), (1,(0,1)), (1,(1,2)), (1,(2,0)), (1,(1,2,0)), (2,(3,)), (3,(4,)), (-1,(3,0)), (-1,(3,1)), (-1,(3,2))]
#print(data)
dp = [[0] + [float("-inf")]*9]
for q in range(qu):
    dp.append([])
    for i in range(10):
        val = float("-inf")
        for a, b in poss:
            #print(a, b)
            temp = [data[q][h] for h in b]
            v = dp[-2][(i-len(b))%10] + sum(temp)
            if i < len(b):
                v += max(temp)
            if v > val:
                val = v
        dp[-1].append(val)

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