t = read_line.to_i
t.times do
  puts solve
end

def solve
  n = read_line.to_i
  sum = 0i64
  cs = Array.new(n) do
    k, l, r = read_line.split.map(&.to_i)
    sum += r
    {(l - r).to_i64, k}
  end.sort
  bit = BIT(Int32).new(n)
  (n - 1).downto(0) do |i|
    d, k = cs[i]
    break if d <= 0
    if bit.cumulative_sum(k) < k
      bit.add(k, 1)
      sum += d
    end
  end
  bit = BIT(Int32).new(n)
  n.times do |i|
    d, k = cs[i]
    k = n - k
    break if d >= 0
    if bit.cumulative_sum(k) < k
      bit.add(k, 1)
    else
      sum += d
    end
  end
  return sum
end

class BIT(T)
  # 1-indexed

  def initialize(size : Int)
    len = 1
    while len < size
      len *= 2
    end
    @v = Array(T).new(len + 1, T.zero)
  end

  def cumulative_sum(index : Int)
    ret = T.zero
    while index > 0
      ret += @v[index]
      index &= index - 1
    end
    ret
  end

  def sum(l : Int, r : Int)
    cumulative_sum(r) - cumulative_sum(l - 1)
  end

  def add(index : Int, val : T)
    while index < @v.size
      @v[index] += val
      index += (index & -index)
    end
  end

  def set(index : Int, val : T)
    old = sum(index, index)
    add(index, val - old)
  end
end
