from heapq import*
i=input
h=heappush
for _ in'_'*int(i()):
 n,s,x,*y=int(i()),0,[]
 for _ in'_'*n:k,l,r=t=[*map(int,i().split())];h(x,t)if l>r else h(y,(n-k,r,l))
 for x in x,y:
  n,*q=len(x),
  while n:
   n-=1
   while x and x[-1][0]>n:_,l,r=x.pop();h(q,(r-l,l,r))
   if q:s+=heappop(q)[1]
  for*_,r in x+q:s+=r
 print(s)