T = int(input())
for _ in range(T):
  N = int(input())
  base = 0
  A = []
  B = [0 for _ in range(N)]
  C =[]
  for i in range(N):
    K, L, R = map(int,input().split()) 
    tmp = min(L,R)
    L -= tmp
    R -= tmp
    if L > R:
      A.append((L,K,0))
    if R >= L:
      A.append((R,K,1))
    base += tmp
  A.sort(reverse = True)
  
  l = 0
  r = N-1
  for a in A:
    if a[2] == 0:
      if l < a[1]:
        l += 1
        base += a[0] 
    else:
      if r >= a[1]:
        r -= 1
        base += a[0]
  print(base)
  
#print(A)