import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


t = int(input())
ANS = [None]*t
from collections import defaultdict
from heapq import heappop as hpp, heappush as hp
for iii in range(t):
    n = int(input())
    lr = [None]*n
    # d1 = defaultdict(list)
    # d0 = defaultdict(list)
    for i in range(n):
        k,l,r = list(map(int, input().split()))
        lr[i] = (l - r, k, l, r, i)
        # if l-r>=0:
        #     d1[k].append((-l+r, l, r, i))
        # else:
        #     d0[k].append((l-r, l, r, i))
            
    lr1 = [item for item in lr if item[0]>=0]
    lr0 = [item for item in lr if item[0]<0]
    lr1.sort(reverse=True)
    lr0.sort()

    bit1 = [0]*(n+1)
    bit0 = [0]*(n+1)
    sl = set()
    sr = set()
    h = []
    for val, k, l, r, i in lr1:
        if k==n:
            sl.add(i)
        elif query(bit1, k)<k:
            sl.add(i)
            add(bit1, k, 1)
        else:
            sr.add(i)
    total = 0
    for val, k, l, r, i in lr0:
        if k==n:
            sl.add(i)
        else:
            if total-query(bit0, k)<(n-k):
                sr.add(i)
                add(bit0, k+1, 1)
                total += 1
            else:
                sl.add(i)
    ans = 0
#     print(lr0, lr1, sl, sr)
    assert len(sl) + len(sr) == n
    for i in sl:
        ans += lr[i][-3]
    for i in sr:
        ans += lr[i][-2]
    ANS[iii] = ans
write("\n".join(map(str, ANS)))