from heapq import *

def solve():
  N=int(input())
  X,Y=[[] for i in range(N)],[[] for i in range(N)]
  k,l,r=0,0,0
  P=0
  for i in range(N):
    k,l,r=map(int,input().split())
    P+=min(l,r)
    if l>=r:
      X[k-1].append(l-r)
    else:
      if k!=N:
        Y[-k].append(r-l)
  Q=[]
  heapify(Q)
  for i in range(N):
    for j in range(len(X[i])):
      heappush(Q,X[i][j])
    while i+1<len(Q):
      heappop(Q)
  while len(Q):
    P+=heappop(Q)
  for i in range(N):
    for j in range(len(Y[i])):
      heappush(Q,Y[i][j])
    while i+1<len(Q):
      heappop(Q)
  while len(Q):
    P+=heappop(Q)
  print(P)

for i in range(int(input())):
  solve()