import sys

input = sys.stdin.buffer.readline

t = int(input())


class SegmentTree:
    ele = 0

    # SEG木は1-index
    # Aに関しては0-index

    def __init__(self, n):  # Aは0-idx
        self.n = n
        self.num = 2 ** ((self.n - 1).bit_length())
        self.SEG = [self.ele] * (2 * self.num)

    def func(self, a, b):
        return a + b

    def search(self, idx):
        return self.SEG[idx + self.num]

    def initialize(self, A):
        for i in range(self.n):
            self.SEG[i + self.num] = A[i]
        for i in range(self.num - 1, 0, -1):
            self.SEG[i] = self.func(self.SEG[2 * i], self.SEG[2 * i + 1])

    def update(self, idx, val):
        idx += self.num
        self.SEG[idx] = val
        idx //= 2
        while idx:
            self.SEG[idx] = self.func(self.SEG[2 * idx], self.SEG[2 * idx + 1])
            idx //= 2

    def query(self, left, right):
        # maspy式。開区間のママ処理する
        # left, rightで値を分けているのは交換法則不成立のときのため
        # 開区間→Rが奇数→右端が偶数→1ずらしてから計算
        # 　下から上への遷移は2で割る
        # juppy氏は閉区間に直していた
        resleft = self.ele
        resright = self.ele
        left += self.num
        right += self.num
        while right - left > 0:
            if left % 2 == 1:
                resleft = self.func(resleft, self.SEG[left])
                left += 1
            if right % 2 == 1:
                right -= 1
                resright = self.func(resright, self.SEG[right])
            left //= 2
            right //= 2
        return self.func(resleft, resright)


def search_upper_smaller(a, SEG):
    ng = a
    ok = n - 1

    if SEG.query(a, n) == n - a:
        return False, 0
    while ok - ng > 1:
        mid = (ok + ng) // 2

        if SEG.query(a, mid) == (mid - a):
            ng = mid
        else:
            ok = mid
    return True, ok


def search_upper_bigger(a, SEG):
    ng = n
    ok = a + 1
    if a == n - 1:
        if SEG.search(n - 1) == 1:
            return False, 0
        else:
            return True, n - 1
    if SEG.query(a + 1, n) == n - a - 1:
        return False, 0
    while ng - ok > 1:
        mid = (ok + ng) // 2

        if SEG.query(mid, n) == (n - mid):
            ng = mid
        else:
            ok = mid
    return True, ok


def search_lower_bigger(a, SEG):
    ng = a + 1
    ok = 0
    if SEG.query(0, a) == a:
        return False, 0
    while ng - ok > 1:
        mid = (ok + ng) // 2
        if SEG.query(mid, a) == (a - mid):
            ng = mid
        else:
            ok = mid
    return True, ok


def search_lower_smaller(a, SEG):
    ng = -1
    ok = a
    if SEG.query(0, a) == a:
        return False, 0
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if SEG.query(0, mid) == (mid - 0):
            ng = mid
        else:
            ok = mid
    return True, ok


for _ in range(t):
    n = int(input())
    kouho = []
    res = 0
    SEG = SegmentTree(n)
    for _ in range(n):
        k, l, r = map(int, input().split())
        if l - r > 0:
            kouho.append([l, r, 1, k - 1])
        elif r - l > 0:
            kouho.append([r, l, 0, k - 1])
        else:
            res += l
        kouho.sort(reverse=True)
    for bigger, smaller, hantei, basho in kouho:
        if hantei:  # 左にいたい
            if SEG.query(0, basho + 1) < basho + 1:
                _, pos = search_lower_bigger(basho, SEG)
                SEG.update(pos, 1)
                res += bigger
            else:
                _, pos = search_upper_bigger(basho, SEG)
                SEG.update(pos, 1)
                res += smaller
        else:  # 右にいたい
            if basho == n - 1:
                _, pos = search_lower_smaller(basho, SEG)
                SEG.update(pos, 1)
                res += smaller
            if SEG.query(basho + 1, n) < n - (basho + 1):
                _, pos = search_upper_smaller(basho, SEG)
                SEG.update(pos, 1)
                res += bigger
            else:
                _, pos = search_lower_smaller(basho, SEG)
                SEG.update(pos, 1)
                res += smaller
    print(res)

