#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


class BinaryIndexedTree(object):

    def __init__(self, n):
        # index is 1-based so vals[0] never be used
        self.n = n
        self.vals = [0] * (n + 1)

    def add(self, i, v):
        while i <= self.n:
            self.vals[i] += v
            i += i & -i

    def sum(self, i):
        ret = 0
        while i > 0:
            ret += self.vals[i]
            i -= i & -i
        return ret

    def sum_range(self, l, r):
        # l and r are 1-based index and inclusive
        return self.sum(r) - self.sum(l - 1)


def solve(N, K, L, R):
    tmps = []
    bit_l = BinaryIndexedTree(N)
    bit_r = BinaryIndexedTree(N)
    for i in range(N):
        #if K[i] >= N:
        #    R[i] = float('inf')
        tmps.append((abs(L[i] - R[i]), K[i], L[i], R[i]))
    tmps.sort(reverse=True)
    ret = 0
    for diff, k, l, r in tmps:
        if l > r:
            s = bit_l.sum(k)
            if s >= k:
                ret += r
            else:
                ret += l
                bit_r.add(k, 1)
        else:
            bit = bit_r
            s = bit_r.sum(N - k)
            if s >= N - k:
                ret += l
            else:
                ret += r
                bit_r.add(N - k, 1)
    print(ret)
    return

def main():
    T = int(input())
    for _ in range(T):
        N = int(input())
        K, L, R = [], [], []
        for i in range(N):
            k, l, r = map(int, input().split())
            K.append(k)
            L.append(l)
            R.append(r)
        solve(N, K, L, R)

if __name__ == '__main__':
    main()
