import sys
input = sys.stdin.readline

t = int(input())
for x in range(t):
  n = int(input())
  plus = []
  minus = []
  res = 0
  for _ in range(n):
    k, l, r = map(int, input().split())
    if l > r:
      res += l
      plus.append((k, r-l))
    elif l < r:
      res += r
      minus.append((-k, l-r))
    else:
      res += l
  plus.sort()
  minus.sort()
  temp = 1
  for k, q in plus:
    if temp <= k:
      temp += 1
    else:
      res += q
  temp = n
  for k, q in minus:
    k = -k
    if temp > k:
      temp -= 1
    else:
      res += q
  print(res)