import sys
from operator import itemgetter
input = sys.stdin.readline


class BIT1():
    """
    Binary Indexed Tree (1-indexed)
    """
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)
        self.data = [0] * (self.n + 1)

    def add(self, idx, x):
        # add x to idx-th element
        # idx: 1-indexed
        self.data[idx] += x
        while idx <= self.n:
            self.bit[idx] += x
            idx += (idx & (-idx))

    def sum(self, idx):
        # get sum of [1, idx]
        # idx: 1-indexed
        s = 0
        while idx:
            s += self.bit[idx]
            idx -= (idx & (-idx))
        return s

    def bisect_left(self, w):
        # condition : always all element is not minus
        # return minimum idx where bit.sum(idx) >= w
        if w <= 0:
            return 0
        idx = 0  # self.bit[idx] < w
        k = 1 << ((self.n).bit_length() - 1)
        while k > 0:
            if idx + k <= self.n and self.bit[idx + k] < w:
                w -= self.bit[idx + k]
                idx += k
            k = k >> 1
        return idx + 1


t = int(input())
for _ in range(t):
    n = int(input())
    camels = [tuple(map(int, input().split())) for _ in range(n)]
    camels.sort(key=lambda x: (abs(x[1] - x[2]) if x[0] < n else 0, x[0]),
                reverse=True)

    # print()
    # print('-' * 10)
    # print(*camels, sep='\n')

    bit = BIT1(n)
    ans = 0
    for i in range(1, n + 1):
        bit.add(i, 1)

    # print(bit.data)
    for k, l, r in camels:
        if k == n or l == r:
            ans += l
        else:
            s = bit.sum(k)
            if l > r:
                if s == 0:
                    # lを使えない
                    idx = bit.bisect_left(s + 1)
                    bit.add(idx, -1)
                    ans += r
                    # print(idx > k)
                else:
                    idx = bit.bisect_left(s)
                    bit.add(idx, -1)
                    ans += l
                    # print(idx <= k)
            elif r > l:
                s2 = bit.sum(n)
                if s2 - s == 0:
                    # rを使えない
                    idx = bit.bisect_left(s)
                    bit.add(idx, -1)
                    ans += l
                    # print(idx <= k)
                else:
                    idx = bit.bisect_left(s + 1)
                    bit.add(idx, -1)
                    ans += r
                    # print(idx > k)
            # print(f'{k}\t {idx}\t {abs(l - r) if k < n else 0}\t {l}\t {r}',
            #       bit.data)

    print(ans)
