from heapq import*

i=input

def f(x):

 x.sort();s,n,*h=0,len(x)

 while n:

  n-=1

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

  if h:s+=heappop(h)[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(x)+f(y)))
from heapq import*

i=input

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

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

 for _ in'_'*n:

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

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

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

 for x in(x,y):

  x.sort();n,*h=len(x),

  while n:

   n-=1

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

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

  for*_,r in x+h:s+=r

 print(s)
from heapq import *

i = input


def f(x):
    x.sort()
    s, n, *h = 0, len(x)
    while n:
        n -= 1
        while x and x[-1][0] > n:
            k, l, r = x.pop()
            heappush(h, (r - l, l, r))
        if h:
            s += heappop(h)[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(x) + f(y)))

from heapq import *

i = input
for _ in "_" * int(i()):
    n, s, x, *y = int(i()), 0, []
    for _ in "_" * n:
        k, l, r = list(map(int, i().split()))
        if l > r:
            x += ((k, l, r),)
        else:
            y += ((n - k, r, l),)
    for x in (x, y):
        x.sort()
        n, *h = (len(x),)
        while n:
            n -= 1
            while x and x[-1][0] > n:
                k, l, r = x.pop()
                heappush(h, (r - l, l, r))
            if h:
                s += heappop(h)[1]
        for *_, r in x + h:
            s += r
    print(s)

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