T = int(input())
for _ in range(T):
  N = int(input())
  LHdef = [[] for j in range(N+1)]
  LHcount = [0 for j in range(N+1)]
  LHsum = 0
  RHdef = [[] for j in range(N+1)]
  RHcount = [0 for j in range(N+1)]
  RHsum = 0
  for i in range(N):
    
    K, L, R = map(int, input().split())
    
    if L >= R:
      #print("L",K, L ,R)
      LHdef[K].append(L-R)
      LHcount[K] += 1
      LHsum += L
    else:
      #print("R", N-K, L ,R)
      RHdef[N-K].append(R-L)
      RHcount[N-K] += 1
      RHsum += R
  dans = 0
  #print(LHdef, LHcount, LHsum)
  #print(RHdef, RHcount, RHsum)
  for i in range(N):
    if i == 0:
      if RHcount[i]>0:
        dans += sum(RHdef[i])
        RHcount[i]=0
    else:
      LHcount[i] = LHcount[i] + LHcount[i-1]
      if LHcount[i]>i:
        dans += sum(sorted(LHdef[i])[:LHcount[i]-i])
        LHcount[i]=i
      RHcount[i] = RHcount[i] + RHcount[i-1]
      if RHcount[i]>i:
        dans += sum(sorted(RHdef[i])[:RHcount[i]-i])
        RHcount[i]=i
  print(RHsum+LHsum-dans)
      
      
  
  