from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi
from operator import mul
from functools import reduce
from pprint import pprint
from copy import deepcopy


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353

class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size
        self.total = 0

    def add(self, i, w):
        x = i + 1
        self.total += w
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res

    def get(self, i):
        if i == 0:
            return self.sum(0)
        return self.sum(i) - self.sum(i - 1)

    def interval_sum(self, i, j): # i <= x < j の区間
        return self.sum(j - 1) - self.sum(i - 1) if i else self.sum(j - 1)

    def search_r(self, l, r):
        #右側にだんだん伸ばしていく
        # l <= x < r
        ng = l - 1
        ok = r - 1
        while ng + 1 < ok:
            mid = (ng + ok) // 2
            if self.interval_sum(l, mid + 1):
                ok = mid
            else:
                ng = mid
        return ok

    def search_l(self, l, r):
        #左側にだんだん伸ばしていく
        # l <= x < r
        ng = r
        ok = l
        while ok + 1 < ng:
            mid = (ok + ng) // 2
            if self.interval_sum(mid, ng):
                ok = mid
            else:
                ng = mid
        return ok

t = I()
for _ in range(t):
    n = I()
    ans = 0
    L = []
    K = []
    for _ in range(n):
        k, l, r = LI()
        diff = l - r
        if diff > 0:
            L += [(diff, k, 1)]
        elif diff < 0:
            L += [(-diff, k, 0)]
        ans += min(l, r)

    L.sort(reverse=True)
    bit2 = BIT(n)
    bit3 = BIT(n)
    for j in range(n):
        bit2.add(j, 1)
        bit3.add(j, 1)
    for d, ki, flg in L:
        if flg:
            if bit2.sum(ki - 1) == 0:
                continue
            else:
                idx = bit2.search_l(0, ki)
                if bit3.get(idx) == 0 and bit3.search_r(idx + 1, n):
                    bit3.add(idx, 1)
                    nxt_idx = bit3.search_r(idx + 1, n)
                    bit3.add(nxt_idx, -1)
                bit2.add(idx, -1)
                ans += d
        else:
            if bit3.interval_sum(ki, n) == 0:
                continue
            else:
                idx2 = bit3.search_r(ki, n)
                if bit2.get(idx2) == 0 and bit2.search_l(0, idx2):
                    bit2.add(idx2, 1)
                    nxt_idx2 = bit2.search_l(0, idx2)
                    bit2.add(nxt_idx2, -1)
                bit3.add(idx2, -1)
                ans += d
    print(ans)





