T=int(input())
class BIT:
    def __init__(self,N):
        self.N=N
        self.data=[0]*(N+1)
    def add(self,i,x):
        i+=1
        while i<=self.N:
            self.data[i]+=x
            i+=i&-i
    def sum(self,a,b):
        if a>0:
            r=self.sum(0,b)-self.sum(0,a)
        else:
            r=0
            i=b
            while i>0:
                r+=self.data[i]
                i-=i&-i
        return r
for t in range(T):
    N=int(input())
    KLR=sorted([list(map(int,input().split())) for i in range(N)],key=lambda x:x[1]-x[2])
    a=0
    b=BIT(N)
    f=0
    for k,l,r in KLR:
        if l==r:
            a+=l
        elif l>r:
            if b.sum(0,k)<k:
                a+=l
                b.add(k-1,1)
            else:
                a+=r
        else:
            if f==0:
                f=1
                b.data=[0]*(N+1)
            if b.sum(k,N)<k<N:
                a+=r
                b.add(k-1,1)
            else:
                a+=l
    print(a)