n=int(input())
total=0
move=0
for i in range(n):
    t=int(input())
    res=[]
    cost=0
    for i in range(t):
        c,d=[int(i) for i in input().split(" ")]
        res.append((c,d))
    res.sort(key=lambda x:(-x[1],x[0]))
    i=0
    while i<t and cost<3:
        if cost==3:
            break
        
        if cost+res[i][0]<=3:
            total=total+res[i][1]
            cost+=res[i][0]
            move+=1
        if move==10:
            total=total+res[i][1]
        i+=1
print(total)    