'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

file=0




def possible(f,x):
    for i in range(x,0,-1):
        if f[i]==0:
            f[i]=1
            return x
    return 0

    
def solve():


    for _ in range(ii()):
    
        n=ii()
        a=[]
        b=[]
        for i in range(n):
            x,y,z=mi()
            a.append([z-y,x,y,z])
        f=[0]*(n+1)
        ans=0
        c=1
        a.sort()
        x=[]
        y=[]
        for i in a:
            if i[0]<0:
                x.append([i[1],i[2],i[3]])
            else:
                y.append([i[1],i[2],i[3]])
        x.sort()
        y.sort(reverse=True)
        c=1
        for i in x:
            c+=1
            if(possible(f,i[0])):
                x1=i[1]
            else:
                x1=i[2]
            ans+=x1
            # print(i,x1)
        for i in y:
            if i[0]<c:
                ans+=i[2]
            else:
                ans+=i[1]
            c+=1
        print(ans)




        
if __name__ =="__main__":

    if(file):

        if path.exists('input1.txt'):
            sys.stdin=open('input1.txt', 'r')
            sys.stdout=open('output1.txt','w')
        else:
            input=sys.stdin.readline
    solve()
