from heapq import heappush,heappop,heapify

pl = []
pr = []

def f(p):
  res = 0
  s = len(p)
  for i in range(s):
    if p[i][0] > s:
      p[i][0] = -s
    else:
      p[i][0] *= -1
  heapify(p)
  hq = []
  for i in range(s,0,-1):
    #print('p:',p,'i:',i)
    while(len(p) > 0 and p[0][0] == -i):
      a,b = heappop(p)
      heappush(hq,-b)
    #print('hq:',hq)
    if len(hq) > 0:
      b = heappop(hq)
      #print('b:',b)
      res -= b
  return res

def solve():
  ans = 0
  n = int(input())
  for i in range(n):
    k,l,r = map(int,input().split())
    m = min(l,r)
    l -= m
    r -= m
    ans += m
    if l > 0:
      pl.append([k,l])
    else:
      pr.append([n-k,r])
  #print('pl:',pl,'pr:',pr,'ans:',ans)
  ans += f(pl)
  ans += f(pr)
  print(ans)

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