t=int(input())
import heapq
dp=[[0]*10 for i in range(t+1)]
def update(i,a):
    l=len(a)
    m=max(a)
    for j in range(10):
        if j+l>=10:
            dp[i][(j+l)%10]=max(dp[i-1][j]+sum(a)+m,dp[i][(j+l)%10])
        else:
            dp[i][(j+l)%10]=max(dp[i-1][j]+sum(a),dp[i][(j+l)%10])



for ti in range(t):
    n=int(input())
    one=[-1,-1,-1]
    two=-1
    thr=-1
    for ni in range(n):
        c,d=map(int,input().split())
        if c==1:
            heapq.heappush(one,d)
            heapq.heappop(one)
        if c==2 and d>two:
            two=d
        if c==3 and d>thr:
            thr=d
    one.sort()
    one=one[::-1]
    ans=[]
    ans.append(list(one))
    ans.append([one[0],max(one[1],two)])
    ans.append([max(one[0],two,thr)])
    for a in ans:
        a.sort()
        if a[0]<1:
            continue
        update(ti+1,a)
print(max(dp[-1]))