import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

T = int(input())

# 1-indexed
class BinaryIndexedTree() :
  def __init__(self, n) :
    self.n = n
    self.tree = [0] * (n + 1)
      
  def sum(self, i) :
    s = 0
    while i > 0 :
      s += self.tree[i]
      i -= i & -i
    return s
      
  def add(self, i, x) :
    while i <= self.n :
      self.tree[i] += x
      i += i & -i

for _ in range(T) :
  N = int(input())
  schedule = []
  for i in range(N) :
    K, L, R = map(int, input().split())
    schedule.append((L, K, 0, i))
    schedule.append((R, K, 1, i))
  schedule.sort(reverse=True)
  
  ret = 0
  BIT = BinaryIndexedTree(N)
  visited = set()
  for v, k, t, i in schedule :
    if i in visited :
      continue
    if t == 0 :
      if BIT.sum(k) < k :
        BIT.add(k, 1)
        visited.add(i)
        ret += v
    if t == 1 :
      if BIT.sum(N) - BIT.sum(k) < k :
        BIT.add(k + 1, 1)
        visited.add(i)
        ret += v
  print(ret)