from heapq import heappop, heappush

t = int(input())
for _ in range(t):
    C = []
    n = int(input())
    for _ in range(n):
        k, l, r = map(int, input().split())
        C.append([k, l, r])
    ans = 0
    left = []
    right = []
    res = 0
    for i in range(n):
        k, l, r = C[i]
        if l > r:
            heappush(left, [C[i][0], i])
        elif r > l:
            heappush(right, [C[i][0], i])
        else:
            ans += r
            res += 1
    cnt = 1
    while left:
        v1, v2 = heappop(left)
        if cnt <= v1:
            ans += C[v2][1]
        else:
            ans += C[v2][2]
        cnt += 1
    while right:
        v1, v2 = heappop(right)
        if cnt <= v1:
            if cnt + res > v1:
                cnt += res
                res = 0
                ans += C[v2][2]
            else:
                ans += C[v2][1]
        else:
            ans += C[v2][2]
        cnt += 1
    print(ans)
