output_description: Print T lines. The i-th line should contain the answer to the i-th test case.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      T
      \mathrm{case}_1
      \vdots
      \mathrm{case}_T
      
  
  Each case is given in the following format:
  
      
      
      N
      K_1 L_1 R_1
      \vdots
      K_N L_N R_N
attempt: from collections import namedtuple
  import sys
  
  input = sys.stdin.readline
  
  Camel = namedtuple("Camel", "k v".split(" "))
  
  
  def read_int():
      return int(input())
  
  
  def read_int_array():
      return [int(ss) for ss in input().split()]
  
  
  class SegmentNode:
      def __init__(self, minidx, maxidx, capacity, occupied, left, right):
          self.minidx = minidx
          self.maxidx = maxidx
          self.capacity = capacity
          self.occupied = occupied
          self.left = left
          self.right = right
  
      def __repr__(self):
          return f"[{self.minidx}-{self.maxidx}, {self.occupied}/{self.capacity}]"
  
      def print_tree(self, lvl=0):
          print((" " * lvl) + self.__repr__())
          if self.left:
              self.left.print_tree(lvl + 2)
          if self.right:
              self.right.print_tree(lvl + 2)
  
      def occupy(self, rightmost_idx):
          if self.minidx > rightmost_idx:
              return False
          if self.capacity == self.occupied:
              return False
          is_leaf = not (self.left or self.right)
          if is_leaf:
              self.occupied += 1
              return True
  
          if self.right:
              result = self.right.occupy(rightmost_idx)
              if result:
                  self.occupied += 1
                  return True
  
          result = self.left.occupy(rightmost_idx)
          if result:
              self.occupied += 1
              return True
          return False
  
  
  def buildtree(size):
      row = [SegmentNode(i, i, 1, 0, None, None) for i in range(size)]
  
      while len(row) > 1:
          newrow = []
          for i in range(0, len(row), 2):
              left = row[i]
              right = row[i + 1] if i + 1 < len(row) else None
              minidx = left.minidx
              maxidx = left.maxidx
              capacity = left.capacity
              if right:
                  maxidx = right.maxidx
                  capacity += right.capacity
              newrow.append(SegmentNode(minidx, maxidx, capacity, 0, left, right))
          row = newrow
  
      return row[0]
  
  
  # st = buildtree(5)
  # print(st.occupy(2))
  # # print(st.occupy(1))
  # st.print_tree()
  
  
  def solution():
      n = read_int()
  
      lefties = []
      righties = []
  
      total = 0
  
      for _ in range(n):
          k, l, r = read_int_array()
          total += min(l, r)
          if l > r:
              lefties.append(Camel(k - 1, l - r))
          if r > l:
              righties.append(Camel(k - 1, r - l))
  
      lefties = sorted(lefties, key=lambda c: -c.v)
      righties = sorted(righties, key=lambda c: -c.v)
  
      ltree = buildtree(len(lefties))
      rtree = buildtree(len(righties))
  
      for l in lefties:
          if ltree.occupy(l.k):
              total += l.v
  
      for r in righties:
          if rtree.occupy(n - r.k - 2):
              total += r.v
  
      return total
  
  
  def main():
      t = read_int()
  
      for i in range(t):
          s = solution()
          print(s)
  
  
  main()
  
  
  # ss = buildtree(100_000)
  #
  # for i in range(50000):
  #     ss.occupy(50000)
  #
  
problem_description: Statement
  
  We have N camels numbered 1,2,\ldots,N. Snuke has decided to make them line up
  in a row.
  
  The happiness of Camel i will be L_i if it is among the K_i frontmost camels,
  and R_i otherwise.
  
  Snuke wants to maximize the total happiness of the camels. Find the maximum
  possible total happiness of the camel.
  
  Solve this problem for each of the T test cases given.
samples: [{"input": "3\n    2\n    1 5 10\n    2 15 5\n    3\n    2 93 78\n    1 71 59\n    3 57 96\n    19\n    19 23 16\n    5 90 13\n    12 85 70\n    19 67 78\n    12 16 60\n    18 48 28\n    5 4 24\n    12 97 97\n    4 57 87\n    19 91 74\n    18 100 76\n    7 86 46\n    9 100 57\n    3 76 73\n    6 84 93\n    1 6 84\n    11 75 94\n    19 15 3\n    12 11 34", "output": "25\n    221\n    1354\n    \n\n  * In the first test case, it is optimal to line up the camels in the order 2, 1.\n    * Camel 1 is not the frontmost camel, so its happiness will be 10.\n    * Camel 2 is among the two frontmost camels, so its happiness will be 15.\n  * In the second test case, it is optimal to line up the camels in the order 2, 1, 3.\n    * Camel 1 is among the two frontmost camels, so its happiness will be 93.\n    * Camel 2 is the frontmost camel, so its happiness will be 71.\n    * Camel 3 is among the three frontmost camels, so its happiness will be 57."}]