import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [-int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()
    DD = ((1,0),(0,1),(-1,0),(0,-1))

    T = NI()
    for z in range(T):

        N = NI()
        c = []

        N0 = 2 ** (N.bit_length())
        st0 = [0] * (2 * N0)
        st1 = [0] * (2 * N0)

        def update_max(i, x):
            i += N0 - 1
            st1[i] = max(st1[i], x)
            while i > 0:
                i = (i - 1) // 2
                st1[i] = max(st1[i * 2 + 1], st1[i * 2 + 2])

        def query_max(l, r):
            L = l + N0;
            R = r + N0
            ret = 0
            while L < R:
                if L % 2:
                    ret = max(ret, st1[L - 1])
                    L += 1
                if R % 2:
                    R -= 1
                    ret = max(ret, st1[R - 1])
                L //= 2;
                R //= 2
            return ret

        def update_min(i, x):
            i += N0 - 1
            st0[i] = min(st0[i], x)
            while i > 0:
                i = (i - 1) // 2
                st0[i] = min(st0[i * 2 + 1], st0[i * 2 + 2])

        def query_min(l, r):
            L = l + N0;
            R = r + N0
            ret = N + 1  # max value
            while L < R:
                if L % 2:
                    ret = min(ret, st0[L - 1])
                    L += 1
                if R % 2:
                    R -= 1
                    ret = min(ret, st0[R - 1])
                L //= 2;
                R //= 2
            return ret

        for i in range(1,N+1):
            update_min(i,i)
            update_max(i,i)

        for _ in range(N):
            k,l,r = LI()
            if l < r:
                c.append((r-l,r,l,-k))
            else:
                c.append((l-r,l,r,k))
        c.sort(reverse=True)
        ans = 0
        for sa,p1,p2,k in c:
            if k > 0:
                u = query_max(1,k+1)
                if u > 0:
                    ans += p1
                    update_max(u,0)
                else:
                    ans += p2
            else:
                u = query_min(abs(k)+1,N+1)
                if u < N+1:
                    ans += p1
                    update_min(u,N+1)
                else:
                    ans += p2
        print(ans)


if __name__ == '__main__':
    main()