import heapq
T = int(input())
for _ in range(T):
   N = int(input())
   left, right = [], []
   ans = 0
   for _ in range(N):
      k,l,r = map(int, input().split())
      if l > r:
         left.append((k-1,l,r))
      elif r > l:
         right.append((N-k-1,l,r))
      else:
         ans += r
   left = sorted(left, key=lambda x: x[0])
   right = sorted(right, key=lambda x: x[0])
   dl = []
   dr = []
   for i,(k,l,r) in enumerate(left):
      ans += l
      heapq.heappush(dl, r-l)
      if len(dl) > k+1:
         ans += heapq.heappop(dl)
   for i,(k,l,r) in enumerate(right):
      ans += r
      heapq.heappush(dr, l-r)
      if len(dr) > k+1:
         ans += heapq.heappop(dr)
   print(ans)