import heapq
def solve():
    N = int(input())
    que = []
    heapq.heapify(que)
    useless = []
    ans = 0
    left = 0
    right = N-1
    Index = [-1 for i in range(N)]
    for i in range(N):
        K, L, R = map(int, input().split())
        if L > R:
            heapq.heappush(que, (-L+R, K, "left"))
        else:
            heapq.heappush(que, (-R+L, K, "right"))
    while que:
        value, index, which = heapq.heappop(que)
        value *= -1
        if which == "left":
            if left <= index-1:
                Index[left] = L
                left += 1
            elif left > index-1:
                # case umpossible
                useless.append(R)
        elif which == "right":
            if right > index-1:
                Index[right] = R
                right -= 1
            elif right <= index-1:
                useless.append(L)
    for i in range(N):
        if Index[i] > 0:
            ans += Index[i]
    ans += sum(useless)
    print(ans)


def main():
    T = int(input())
    for i in range(T):
        solve()


if __name__ == "__main__":
    main()