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()) 
    L -= min(L,R)
    R -= min(L,R)
    if L > R:
      A.append((L,K,0))
    if L > R:
      A.append((R,K,1))
    base += min(L,R)
  A.sort(reverse = True)
  
  l = 0
  r = N-1
  for a in A:
    if a[2] == 0:
      if l < K:
        l += 1
        base += L 
    else:
      if r >= K:
        r -= 1
        base += R
  print(base)