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
  used = Array.new(n + 1, false)
  (n - 1).downto(0) do |i|
    d, k = cs[i]
    break if d < 0
    while used[k]
      k -= 1
    end
    if k > 0
      used[k] = true
      sum += d
    end
  end
  used.fill(false)
  n.times do |i|
    d, k = cs[i]
    k = n - k
    break if d >= 0
    while used[k]
      k -= 1
    end
    if k > 0
      used[k] = true
    else
      sum += d
    end
  end
  return sum
end
