import sys
input = lambda: sys.stdin.readline().rstrip()
class SegAccumCalc:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def update(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new
    
    def at(self, n):
        return self.node[(self.N-1)+n]

    def query(self, l=0, r=-1):
        if r < 0: r = self.N
        L = l + self.N; R = r + self.N
        s = None
        while L < R:
            if R & 1:
                R -= 1
                s = self.calc(self.node[R-1], s)
            if L & 1:
                s = self.calc(self.node[L-1], s)
                L += 1
            L >>= 1; R >>= 1
        return s

inpl = lambda: list(map(int,input().split()))
T = int(input())
for t in range(T):
    N = int(input())
    base = 0
    KD = []
    for i in range(N):
        K, L, R = inpl()
        base += R
        KD.append((K, L-R))
    KD.sort(key=lambda x: x[1], reverse=True)
    sac = SegAccumCalc([0]*N)
    score = base
    for i in range(N):
        K, D = KD[i]
        if sac.query(0,K) < K:
            sac.update(K-1,sac.at(K-1)+1)
            score += D
    print(score)
