cdef solve():
    N = int(input())
    camels = [tuple(map(int, input().split())) for _ in range(N)]

    camels.sort(key=lambda x: abs(x[1] - x[2]), reverse=True)

    L, R = [False] * N, [False] * N
    full_idx_l, full_idx_r = 0, N - 1
    ans = 0
    for k, l, r in camels:
        if l > r:
            k -= 1
            if k < full_idx_l:
                ans += r
                continue
            while L[k]:
                k -= 1
                if k < full_idx_l:
                    ans += r
                    continue
            L[k] = True
            ans += l
            while full_idx_l < N and L[full_idx_l]:
                full_idx_l += 1
        else:
            if k > full_idx_r:
                ans += l
                continue
            while R[k]:
                k += 1
                if k > full_idx_r:
                    ans += l
                    continue
            R[k] = True
            ans += r
            while full_idx_r >= 0 and R[full_idx_r]:
                full_idx_r -= 1
    print(ans)


T = int(input())
for _ in range(T):
    solve()
