#!/usr/bin/env python3

import heapq
import sys
input=sys.stdin.readline

t=int(input())
for _ in range(t):
    n=int(input())
    arr=[list(map(int,input().split())) for _ in range(n)]
    head=[]
    tail=[]
    for k,l,r in arr:
        if l>=r or k==n:
            head.append((k,l,r))
        else:
            tail.append((k,l,r))
    head=sorted(head,key=lambda x:x[0])
    q=[]
    cnt=0
    ans=[]
    for k,l,r in head:
        heapq.heappush(q,(l-r,l))
        cnt+=1
        while cnt>k:
            tmp,val=heapq.heappop()
            ans.append((tmp,val-tmp))
            cnt-=1
    ans+=list(q)
    cnt=len(ans)
    tail2=[]
    for k,l,r in tail:
        if k<=cnt:
            ans.append((l-r,r))
        else:
            tail2.append((k,l,r))
    tail2=sorted(tail2,reverse=True,key=lambda x:x[0])
    q2=[]
    cnt2=0
    for k,l,r in tail2:
        heapq.heappush(q2,(r-l,r))
        cnt2+=1
        while cnt2>(n-k+1):
            tmp,val=heapq.heappop()
            ans.append((tmp,val-tmp))
            cnt-=1
    ans+=list(q2)
    ret=0
    for _,val in ans:
        ret+=val
    print(ret)