from heapq import heappop, heappush

t = int(input())
for i in range(t):
  n = int(input())
  klr = [list(map(int, input().split())) for i in range(n)]
  plus = []
  p_que = []
  minus = []
  m_que = []
  ans = 0
  for key, (k,l,r) in enumerate(klr):
    diff = l-r
    if diff >= 0:
      plus.append([k, diff, key])
      heappush(p_que,(diff, k, key))
      ans += r
    else:
      minus.append([k, -diff, key])
      heappush(m_que,(-diff, k, key))
      ans += l
  plus.sort(reverse=True)
  used = [0] * n
  now = len(plus)
  for k,diff,key in plus:
    while now > k:
      data = heappop(p_que)
      idx = data[2]
      if used[idx]:
        continue
      used[idx] = 1
      now -= 1
    if used[key]:
      continue
    used[key] = 1
    ans += diff
    now -= 1
  assert now == 0
  now = len(plus) + 1      
  minus.sort()
  for k,diff,key in minus:
    while now <= k:
      data = heappop(m_que)
      idx = data[2]
      if used[idx]:
        continue
      used[idx] = 1
      now += 1
    if used[key]:
      continue
    used[key] = 1
    ans += diff
    now += 1
  print(ans)  