
t = int(input())

nlist = []
klists = []

for i in range(t):
    n = int(input())
    nlist.append(n)
    klist = []
    for j in range(n):
        klist.append(input())
    klists.append(klist)

for i in range(t):
    n = nlist[i]
    klist = klists[i]

    l_list = []
    r_list = []
    for j in range(n):
        k,l,r = map(int,klist[j].split())
        if l-r > 0:
            l_list.append([k,l,r])
        else: 
            r_list.append([k,l,r])
    

    l_list.sort(key=lambda x: (x[1]-x[2]),reverse=True)
    l_list.sort(key=lambda x: x[0])
    r_list.sort(key=lambda x: (x[1]-x[2]),reverse=True)
    r_list.sort(key=lambda x: x[0])

    l_list.extend(r_list)

    sort_list = l_list
    ans_list = []
    temp_list = []
    for j in range(n):
        while sort_list  != [] and sort_list[0][0] <= j+1:
            temp_list.append(sort_list[0])
            sort_list.pop(0)

        temp_list.sort(key=lambda x: (x[1]-x[2]),reverse=True)

        while temp_list != [] and len(ans_list)<j+1:
            ans_list.append(temp_list[0])
            temp_list.pop(0)
    
    while temp_list != []:
        ans_list.append(temp_list[0])
        temp_list.pop(0)
    
    sum = 0
    for j in range(n):
        if ans_list[j][0] >= j+1:
            sum += ans_list[j][1]
        else:
            sum += ans_list[j][2]
    print(sum)
