from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class BitSum:
    def __init__(self, n):
        self.n = n + 1
        self.table = [0] * self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

for _ in range(II()):
    n=II()
    hp=[]
    hpr=[]
    ans=0
    for _ in range(n):
        k,l,r=MI()
        d=r-l
        if d<0:heappush(hp,(r-l,l,r,k))
        elif d>0:heappush(hpr,(-r+l,l,r,k))
        else:ans+=r

    bit=BitSum(n+3)
    while hp:
        d,l,r,k=heappop(hp)
        if bit.sum(k)<k:
            ans+=l
            bit.add(k,1)
        else:
            ans+=r

    bit=BitSum(n+3)
    while hpr:
        d,l,r,k=heappop(hpr)
        k=n-k
        if bit.sum(k)<k:
            ans+=r
            bit.add(k,1)
        else:
            ans+=l

    print(ans)
