from collections import namedtuple
from pprint import pp

Camel = namedtuple("Camel", "k l r crt preference avail".split(" "))

def read_int():
    return int(input())

def read_int_array():
    return [int(ss) for ss in input().split()]

def solution():
    n = read_int()
    camels = []

    for _ in range(n):
        k, l, r = read_int_array()
        preference = None
        if l > r:
            preference = "l"
        elif l < r:
            preference = "r"
        camels.append(Camel(k - 1, l, r, None, preference, None))

    camels = sorted(camels, key = lambda c: -abs(c.l - c.r))

    arrangement = [None for _ in camels]

    lefties = [None for _ in camels]
    righties = [None for _ in camels]

    lcount = 0
    rcount = 0

    total_happy = 0

    for c in camels:
        if c.preference is None:
            total_happy += c.r

        elif c.preference == "l":
            total_happy += c.r
            for i in range(c.k, -1, -1):
                if lefties[i] is None:
                    lefties[i] = c
                    lcount += 1
                    total_happy += c.l - c.r
                    break

        else: # "r"
            total_happy += c.l
            for i in range(c.k + 1, n):
                if righties[i] is None:
                    righties[i] = c
                    rcount += 1
                    total_happy += c.r - c.l
                    break

    return total_happy

def main():
    t = read_int()

    for i in range(t):
        s = solution()
        print(s)


main()
