T = int(input())


def solve(camels):
    plus = 0
    camels.sort(reverse=True)
    S = []
    for i in range(len(camels), 0, -1):
        if i == len(camels):
            for camel in camels:
                k = camel[0]
                lr = camel[1]
                if k > i:
                    S.append(lr)
        for camel in camels:
            k = camel[0]
            lr = camel[1]
            if k == i:
                S.append(lr)
            if k < i:
                break

        if S:
            S.sort(reverse=True)
            plus += S.pop(0)
    return plus

    
for i in range(T):
    N = int(input())

    score = 0
    l_camel = []
    r_camel = []
    for _ in range(N):
        k, l, r = map(int, input().split())
        if l > r:
            l_camel.append([k, l-r])
            score += r
        elif l < r:
            r_camel.append([N-k, r-l])
            score += l
        else:
            score += r
    score += solve(l_camel)
    score += solve(r_camel)
    print(score)