class MyHeapQ
  def initialize(array = [])
    @data = []
    array.each{|a| push(a)}
  end

  def push(element)
    @data.push(element)
    bottom_up
  end

  def pop
    if size == 0
      return nil
    elsif size == 1
      return @data.pop
    else
      top = @data[0]
      @data[0] = @data.pop
      top_down
      return top
    end
  end

  def size
    @data.size
  end

  def data
    @data
  end

  private

  def swap(i, j)
    @data[i], @data[j] = @data[j], @data[i]
  end

  def parent_idx(target_idx)
    (target_idx - 1) / 2
  end

  def bottom_up
    target_idx = size - 1
    return if target_idx == 0
    parent_idx = parent_idx(target_idx)
    while ((@data[parent_idx][1] <=> @data[target_idx][1])>0)
      swap(parent_idx, target_idx)
      break if parent_idx == 0
      target_idx = parent_idx
      parent_idx = parent_idx(target_idx)
    end
  end

  def top_down
    target_idx = 0

    # child がある場合
    while (target_idx * 2 + 1 < @data.size)	# has_child?

      a = target_idx * 2 + 1		# left_child_idx
      b = target_idx * 2 + 2		# right_child_idx

      if @data[b].nil?
        c = a
      else
        c = (@data[a][1] <=> @data[b][1])<1 ? a : b
      end

      if (@data[target_idx][1] <=> @data[c][1])>0
        swap(target_idx, c)
        target_idx = c
      else
        return
      end
    end
  end
end

t = gets.strip.to_i
t.times do
  n = gets.strip.to_i
  klr = []
  s = []
  m = []
  ans = 0
  n.times do |i|
    k,l,r = gets.strip.split.map(&:to_i)
    if l == r
      ans += l
    elsif l > r
      s.push [k, l-r]
      ans += r
    else
      m.push [n-k, r-l]
      ans += l
    end
  end
  s.sort!
  m.sort!
  q = MyHeapQ.new
  c = 1
  s.each do |e|
    if e[0] > c
      while q.size > c
        q.pop
      end
      c = e[0]
    end
    q.push e
  end
  while q.size > c
    q.pop
  end
  while q.size > 0
    ans += q.data.pop[1]
  end
  c = 0
  m.each do |e|
    if e[0] > c
      while q.size > c
        q.pop
      end
      c = e[0]
    end
    q.push e
  end
  while q.size > c
    q.pop
  end
  while q.size > 0
    ans += q.data.pop[1]
  end
  p ans
end
