turns = int(input())
damageDealt = 0
cardsPlayed = 0

for turn in range(turns):
    newCards = int(input())
    ones = [0,0,0]
    twos = [0,0,0]
    threes = [0,0,0]
    for _ in range(newCards):
        c, d = map(int, input().strip().split())
        if c == 1:
            ones.append(d)
        elif c == 2:
            twos.append(d)
        else:
            threes.append(d)
    ones.sort(reverse=True)
    twos.sort(reverse=True)
    threes.sort(reverse=True)
    #print(ones)
    options = [threes[0], twos[0] + ones[0], sum(ones[:3])]
    #print(options)
    bestOption = max(options)
    #print('The best option is', bestOption)
    if options[0] == bestOption:
        if cardsPlayed % 10 == 9:
            damageDealt += bestOption * 2
        else:
            damageDealt = bestOption
        cardsPlayed += 1
    elif options[1] == bestOption:
        a = max(twos[0], ones[0])
        b = min(twos[0], ones[0])
        if b > 0 and cardsPlayed % 10 == 8:
            damageDealt += a * 2 + b
        elif cardsPlayed % 10 == 9:
            damageDealt += a * 2 + b
        else:
            damageDealt += a + b

        if b:
            cardsPlayed += 2
        else:
            cardsPlayed += 1
    else:
        a = ones[0]
        b = ones[1]
        c = ones[2]
        if c and cardsPlayed % 10 >= 7:
            damageDealt += a * 2 + b + c
        elif b and cardsPlayed % 10 >= 8:
            damageDealt += a * 2 + b + c
        elif cardsPlayed % 10 == 9:
            damageDealt += a * 2 + b + c
        else:
            damageDealt += a + b + c
        if c:
            cardsPlayed += 3
        elif b:
            cardsPlayed += 2
        else:
            cardsPlayed += 1
    #print(damageDealt)

print(damageDealt)
