import sys

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]

def solve():
    N = int(input())
    L = []
    R = []
    base = 0
    for i in range(N):
        k,l,r = list(map(int,input().split()))
        if l>r:
            L.append((l-r,k))
            base+=r
        else:
            R.append((r-l,N-k))
            base+=l
    L.sort(key = lambda x:x[0],reverse=True)
    L.sort(key = lambda x:x[1])
    l_ans = 0
    l_count = 0
    for i in range(len(L)):
        score,k = L[i]
        if k>l_count:
            l_count+=1
            l_ans+=score

    R.sort(key = lambda x:x[0],reverse=True)
    R.sort(key = lambda x:x[1])
    r_ans = 0
    r_count = 0
    for i in range(len(R)):
        score,k = R[i]
        if k>r_count:
            r_count+=1
            r_ans+=score
    print(base+l_ans+r_ans)


def main():
    T = int(input())

    for _ in range(T):
        solve()
        
if __name__ == '__main__':
    main()


