import sys
from heapq import heappop, heappush
def main():
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    l_camel = [[] for _ in range(n)]
    r_camel = [[] for _ in range(n)]
    ans = 0
    for i in range(n):
        k, l, r = map(int, input().split())
        if l >= r:
            ans += r
            l_camel[k-1].append(l-r)
        else:
            ans += l
            r_camel[k-1].append(r-l)
    
    table = [True]*n
    cur = 0
    for i in range(n):
        while l_camel[i]:
            node = heappop(l_camel[i])
            if cur <= i:
                cur += 1
                ans += node
    
    cur = n-1
    for i in range(n-1, -1, -1):
        while r_camel[i]:
            node = heappop(r_camel[i])
            if cur > i:
                cur -= 1
                ans += node
    print(ans)


if __name__ == '__main__':
    t = int(input())
    for i in range(t):
        main()