#!/usr/bin/env python
#pyrival orz
import os
import sys
from io import BytesIO, IOBase

"""
    for _ in range(int(input())):
    n,m=map(int,input().split())
    n=int(input())
    a = [int(x) for x in input().split()]
"""

class SegmentTree:
    def __init__(self, data, default=0, func=max):
        """initialize the segment tree with data"""
        self._default = default
        self._func = func
        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = self._default

    def __getitem__(self, idx):
        return self.data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        self.data[idx] = value
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        res_left = res_right = self._default
        while start < stop:
            if start & 1:
                res_left = self._func(res_left, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res_right = self._func(self.data[stop], res_right)
            start >>= 1
            stop >>= 1

        return self._func(res_left, res_right)

    def __repr__(self):
        return "SegmentTree({0})".format(self.data)


def main():

    N,K=2000000,2000000
    for _ in range(int(input())):
        n=int(input())
        ans=0
        pv,nv=[],[]
        for _ in range(n):
            k,l,r=map(int,input().split())
            ans+=r
            l-=r
            if l>=0:
                pv.append((k,l))
            else:
                nv.append(l*N*K+k*K)
                # print(k,l)

        gt=[i for i in range(len(pv))]
        gt.sort(key=lambda v:pv[v][1])

        aa=SegmentTree([i for i in range(n)])

        for i in gt:
            k,l=pv[i]
            mx=aa.query(0,k)
            if mx<0:
                continue
            ans+=l
            aa[mx]=-1

        nv.sort()
        for i in range(len(nv)):
            nv[i]+=i
        gt=[i for i in range(len(nv))]
        gt.sort(key=lambda v:(nv[v]//K)%N)
        cnt=len(pv)
        vis=[True]*len(nv)
        aa=SegmentTree(nv,default=-1<<100)
        for i in gt:
            k=(nv[i]//K)%N
            # print("k",k,cnt)
            while cnt<k and vis[i]:
                mn=aa.query(0,len(nv))%K
                ans+=nv[mn]//N//K
                # print("mn",mn,i,k,cnt,nv[mn]//N//K)
                vis[mn]=False
                aa[mn]=(-1<<100)
                cnt+=1

            if vis[i]:
                cnt+=1
            vis[i]=False
            aa[i]=(-1<<100)
            # print(nv)
            # print(" ".join( str(aa.query(iii,iii+1)) for iii in range(len(nv))))
        print(ans)

# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()