import heapq
T=int(input())
for case in range(T):
    N=int(input())
    KLR=[]
    ans=0
    for i in range(N):
        k,l,r=map(int,input().split())
        ans+=min(l,r)
        KLR.append((k,l,r))
    KLR.sort(key=lambda x:x[0])
    KLR.append((N+1,0,0))
    q=[]
    index=0
    for j in range(1,N+1):
        while(1):
            k,l,r=KLR[index]
            if k==j:
                heapq.heappush(q,(l-r,k,l,r))
                index+=1
            else:
                break
        while(len(q)>j):
            lr,k,l,r=heapq.heappop(q)
    while(len(q)>0):
        lr,k,l,r=heapq.heappop(q)
        ans+=abs(lr)
    print(ans)