from heapq import*

i=input

s=sorted

def f(x):

 s,n,*h=0,len(x)

 while n:

  while x and x[-1][0]>=n:k,l,r=x.pop();heappush(h,(r-l,l,r))

  if h:s+=heappop(h)[1]

  n-=1

 return s+sum(r for _,_,r in x+h)

for _ in'_'*int(i()):

 n,x,*y=int(i()),[]

 for _ in'_'*n:

  k,l,r=list(map(int,i().split()))

  if l>r:x+=(k,l,r),

  else:y+=(n-k,r,l),

 print((f(s(x))+f(s(y))))
from heapq import*

i=input

s=sorted

def f(x):

 s,n,*h=0,len(x)

 while n:

  while x and x[-1][0]>=n:k,l,r=x.pop();heappush(h,(r-l,l,r))

  if h:s+=heappop(h)[1]

  n-=1

 return s+sum(r for*_,r in x+h)

for _ in'_'*int(i()):

 n,x,*y=int(i()),[]

 for _ in'_'*n:

  k,l,r=list(map(int,i().split()))

  if l>r:x+=(k,l,r),

  else:y+=(n-k,r,l),

 print((f(s(x))+f(s(y))))
from heapq import *

i = input
s = sorted


def f(x):
    s, n, *h = 0, len(x)
    while n:
        while x and x[-1][0] >= n:
            k, l, r = x.pop()
            heappush(h, (r - l, l, r))
        if h:
            s += heappop(h)[1]
        n -= 1
    return s + sum(r for _, _, r in x + h)


for _ in "_" * int(i()):
    n, x, *y = int(i()), []
    for _ in "_" * n:
        k, l, r = list(map(int, i().split()))
        if l > r:
            x += ((k, l, r),)
        else:
            y += ((n - k, r, l),)
    print((f(s(x)) + f(s(y))))

from heapq import *

i = input
s = sorted


def f(x):
    s, n, *h = 0, len(x)
    while n:
        while x and x[-1][0] >= n:
            k, l, r = x.pop()
            heappush(h, (r - l, l, r))
        if h:
            s += heappop(h)[1]
        n -= 1
    return s + sum(r for *_, r in x + h)


for _ in "_" * int(i()):
    n, x, *y = int(i()), []
    for _ in "_" * n:
        k, l, r = list(map(int, i().split()))
        if l > r:
            x += ((k, l, r),)
        else:
            y += ((n - k, r, l),)
    print((f(s(x)) + f(s(y))))

-    return s + sum(r for _, _, r in x + h) +    return s + sum(r for *_, r in x + h)