import sys
sys.setrecursionlimit(10 ** 8)

def input(): return sys.stdin.readline().strip()
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    T = Z()
    ans = []
    for _ in range(T):
        N = Z()
        camel1 = []
        camel2 = []
        for _ in range(N):
            k, l, r = ZZ()
            if l > r: camel1.append([k, -l, r])
            else: camel2.append([-k, -r, l])
        camel1.sort()
        camel2.sort()
        order = 1
        cc = 0
        for i in range(len(camel1)):
            k, l, r = camel1[i]
            l = -l
            if order <= k: cc += l
            else: cc += r
            order += 1
        order = N
        for i in range(len(camel2)):
            k, r, l = camel2[i]
            k = -k
            r = -r
            if order <= k: cc += l
            else: cc += r
            order -= 1

        ans.append(cc)

    for a in ans:
        print(a)

    return

if __name__ == '__main__':
    main()
