require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
class PQ
  attr_reader :elements
  def initialize(array = nil, &block)
    @elements = [nil]
    array.each{|e| push(e)} if array
    @compare_func = block
  end

  def push(element)
    @elements << element
    bubble_up(@elements.size - 1)
  end

  def pop
    swap(1, @elements.size - 1)
    max = @elements.pop
    bubble_down(1)
    max
  end

  def size() @elements.size - 1 end
  def top() @elements[1] end
  def empty?() @elements.size == 1 end

  private

  def compare(a, b)
    @compare_func.call(a, b)
  end

  def bubble_up(idx)
    parent_idx = idx / 2
    return if idx <= 1
    return if compare(@elements[parent_idx], @elements[idx])

    swap(idx, parent_idx)
    bubble_up(parent_idx)
  end

  def bubble_down(idx)
    child_idx = (idx * 2)
    return if child_idx > @elements.size - 1

    not_the_last_element = child_idx < @elements.size - 1
    left_element = @elements[child_idx]
    right_element = @elements[child_idx + 1]
    child_idx += 1 if not_the_last_element && compare(right_element, left_element)

    return if compare(@elements[idx], @elements[child_idx])

    swap(idx, child_idx)
    bubble_down(child_idx)
  end

  def swap(source, target)
    @elements[source], @elements[target] = @elements[target], @elements[source]
  end
end
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i

  left  = []
  right = []
  ans = 0
  n.times do
    k, l, r = gets.chomp.split.map(&:to_i)
    ans += [l, r].min
    if l >= r
      left.push [k, l-r]
    else
      right.push [k, r-l]
    end
  end

  h = Hash.new{|h,k| h[k] = []}
  left.each do |k, lr|
    h[k].push lr
  end

  # p h
  pq = PQ.new{|a,b| a > b}
  n.downto(1) do |i|
    h[i].each{|num| pq.push num}
    ans += pq.pop unless pq.empty?
  end

  h = Hash.new{|h,k| h[k] = []}
  right.each do |k, lr|
    h[k].push lr
  end

  # p h
  pq = PQ.new{|a,b| a > b}
  1.upto(n) do |i|
    h[i-1].each{|num| pq.push num}
    ans += pq.pop unless pq.empty?
  end

  puts ans
end
