T = int(input())
for _ in range(T):
   N = int(input())
   left, right = [], []
   for _ in range(N):
      k,l,r = map(int, input().split())
      if l > r:
         left.append((k-1,l,r))
      else:
         right.append((N-k-1,l,r))
   left = sorted(left, key=lambda x: x[2]-x[1])
   right = sorted(right, key=lambda x: x[1]-x[2])
   ans = 0
   dl = {}
   dr = {}
   for i,(k,l,r) in enumerate(left):
      while k in dl and k >= 0:
         k -= 1
      if k == -1:
         ans += r
      else:
         dl[k] = i
         ans += l
   for i,(k,l,r) in enumerate(right):
      while k in dr and k >= 0:
         k -= 1
      if k == -1:
         ans += l
      else:
         dr[k] = i
         ans += r
   # for i,(k,l,r) in enumerate(left):
   #    ans += l if i <= k else r
   # for i,(k,l,r) in enumerate(right):
   #    ans += r if i < k else l
   print(ans)