class BinaryIndexedTree:
    def __init__(self,sz):
        self.sz = sz
        self.data = [0]*(sz+1)
    def sum(self, *args):
        if len(args) == 1:
            # [0, k]
            res = 0
            k = args[0] + 1
            while k > 0:
                res += self.data[k]
                k -= k & -k
            return res
        elif len(args) == 2:
            # [l, r)
            return self.sum(args[1]-1) - self.sum(args[0]-1)
    def add(self, k, x):
        k += 1
        while k <= self.sz:
            self.data[k] += x
            k += k & -k
import heapq
t = int(input())
while t:
    t-=1
    n = int(input())
    klr = [list(map(int, input().split())) for i in range(n)]
    for i in range(n):
        klr[i][0] -= 1

    ans = 0
    diff = []
    posl = BinaryIndexedTree(n)
    posr = BinaryIndexedTree(n)
    for i in range(n):
        K = klr[i][0]
        L = klr[i][1] 
        R = klr[i][2]
        if L >= R:
            posl.add(K,1)
            ans += L
            diff.append((L-R,'l',i))
        else:
            posr.add(K,1)
            ans += R
            diff.append((R-L,'r',i))

    heapq.heapify(diff)

    for i in range(n):
        d,pos,idx = heapq.heappop(diff)
        K = klr[idx][0]
        if pos =='l' and posl.sum(K)>K+1:
            ans -= d
            posl.add(K,-1)
            posr.add(K,1)
        elif pos =='r' and posr.sum(K,n)>n-K-1:
            ans -= d
            posr.add(K,-1)
            posl.add(K,1)
    print(ans)
