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


t = int(input())
val = [None]*t
from collections import defaultdict
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)
        if l-r>=0:
            d1[k].append((l-r, l, r, i))
        else:
            d0[k].append((l-r, l, r, i))
            
    
    sl = set()
    sr = set()
#     for i, (v, kk,_,_) in enumerate(lr):
    for kk in range(1, n+1):
        if not kk in d1:
            continue
        ll = d1[kk]
        ll.sort(reverse=True)
        for _, l, r, i in ll:
            if len(sl)<kk:
                sl.add(i)
            else:
                sr.add(i)
    for kk in range(n, 0, -1):
        if not kk in d0:
            continue
        ll = d0[kk]
        ll.sort()
        for _, l, r, i in ll:
            if len(sr)<(n-kk):
                sr.add(i)
            else:
                sl.add(i)
    ans = 0
    for i in sl:
        ans += lr[i][-2]
    for i in sr:
        ans += lr[i][-1]
    val[iii] = ans
write("\n".join(map(str, val)))