from heapq import*
def solve(x):
    s,*h=0,
    for i in range(len(x),0,-1):
        while x and x[-1][0]>=i:
            k,l,r=x.pop()
            heappush(h,(r-l,l,r))
        if h:s+=heappop(h)[1]
    return s+sum(r for _,_,r in x+h)
for _ in range(int(input())):
    n=int(input())
    x,y=[],[]
    for _ in range(n):
        k,l,r=map(int,input().split())
        if l>r:x+=(k,l,r),
        else:y+=(n-k,r,l),
    print(solve(sorted(x))+solve(sorted(y)))