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,l,r))
   left = sorted(left, key=lambda x: x[0])
   right = sorted(right, key=lambda x: x[0])
   # print(left, right)
   ans = 0
   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)