from collections import defaultdict
from itertools import accumulate
import sys
input = sys.stdin.readline

T = int(input())
for _ in range(T):
    N = int(input())
    # front = defaultdict(list)  # int/bool/list....
    front = defaultdict(list)
    back = defaultdict(list)
    cnt = [0] * (N + 1)
    f_cnt = [0] * (N + 1)
    b_cnt = [0] * (N + 1)
    ans = 0
    for i in range(N):
        K, L, R = [int(x) for x in input().split()]
        if L >= R or K == N:
            front[K].append(tuple((L, R)))
            f_cnt[K] += 1
        else:
            back[K + 1].append(tuple((L, R)))
            b_cnt[K + 1] += 1
        # cnt[K] += 1

    b_cum = 0
    for i in range(N, 0, -1):   # 後ろから
        if b_cnt[i] == 0: continue
        if b_cum + b_cnt[i] <= N + 1 - i: # 希望を満たせる
            for l, r in back[i]:
                ans += r
            b_cum += b_cnt[i]
        else:
            drop = b_cum + b_cnt[i] - (N + 1 - i)  # 希望を満たせないラクダの数
            back[i].sort(key = lambda x : x[1] - x[0])  # 落としても良い順
            for j in range(drop):
                ans += back[i][j][0]
            for j in range(drop, b_cnt[i]):
                ans += back[i][j][1]
            b_cum = N + 1 - i # 希望を満たしたラクダの数

    f_cum = 0
    for i in range(1, N + 1):   # 前から
        if f_cnt[i] == 0: continue
        if f_cum + f_cnt[i] <= i: # 希望を満たせる
            for l, r in front[i]:
                ans += l
            f_cum += f_cnt[i]
        else:
            drop = f_cum + f_cnt[i] - i  # 希望を満たせないラクダの数
            front[i].sort(key = lambda x : x[0] - x[1])  # 落としても良い順
            for j in range(drop):
                ans += front[i][j][1]
            for j in range(drop, f_cnt[i]):
                ans += front[i][j][0]
            f_cum = i # 希望を満たしたラクダの数

    print(ans)