# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
import math

def solve():
    N = int(sysread())
    KLR_left, KLR_right = [], []
    counts_l, counts_r = [0] * (N+1), [0] * (N+1)

    for _ in range(N):
        k,l,r = map(int, sysread().split())
        if l>r:
            KLR_left.append((k, l, r))
            counts_l[k] += 1
        else:
            KLR_right.append((k, l, r))
            counts_r[k] += 1

    KLR_left = sorted(KLR_left, key = lambda x:x[0], reverse=True)
    KLR_right = sorted(KLR_right, key=lambda x:x[0])
    queue_left = []
    queue_right = []
    dump = []
    SUM = 0
    for i in range(1, N+1):
        for j in range(counts_l[i]):
            k, l, r = KLR_left.pop()
            heappush(queue_left, (l-r, l, r))
            SUM += l

        while True:
            if len(queue_left)>i:
                l_r, l, r = heappop(queue_left)
                dump.append((l_r, l, r))
                SUM -= l_r
            else:break


    #print(filled_left)
    for i in list(range(1, N+1))[::-1]:
        for j in range(counts_r[i]):
            k, l, r = KLR_right.pop()
            heappush(queue_right, (r-l, l, r))
            SUM += r
        while True:
            if len(queue_right) > N-i:
                try:
                    r_l, l, r = heappop(queue_right)
                except:break
                dump.append((r_l, l, r))
                SUM -= r_l
            else:break
    return SUM

def run():
    T = int(sysread())
    for i in range(T):
        print(solve())



if __name__ == "__main__":
    run()
