import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  _n=I()

  sm=0
  mx=0
  for _ in range(_n):
    l1=[0,0,0]
    l2=[0,0,0]
    l3=[0,0,0]
    n=I()
    for i in range(n):
      a,b=LI()
      if a==1:
        l1.append(b)
      elif a==2:
        l2.append(b)
      else:
        l3.append(b)

    l1=sorted(l1,key=lambda x:x,reverse=True)
    l2=sorted(l2,key=lambda x:x,reverse=True)
    l3=sorted(l3,key=lambda x:x,reverse=True)

    # sm+=max(sum(l1[:3]),l1[0]+l2[0],l3[0])
    p=sum(l1[:3])
    q=l1[0]+l2[0]
    r=l3[0]

    if p>q and p>r:
      sm+=p
      mx=max(mx,l1[0])
    elif q>=p and q>r:
      sm+=q
      mx=max(mx,max(l1[0],l2[0]))
    else:
      sm+=r
      mx=max(mx,r)

  return sm+mx

print(main())
