#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']
require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

class Hp_neg
  def initialize(len = 200000)
    @main = na(len,[99999999,nil])
    @ind = 1
  end

  def push(n)
    @main[@ind] = n
    sind = @ind
    while(sind >= 2)
      nind = sind/2
      if(@main[sind][0] < @main[nind][0])
        @main[sind], @main[nind] = @main[nind], @main[sind]
      else
        break
      end
      sind /= 2
    end
    @ind += 1
  end

  def put()
    return @main
  end

  def pop()
    ans = @main[1]
    @ind -= 1
    @main[1] = @main[@ind]
    @main[@ind] = [99999999,nil]
    sind = 1
    while(true)
      nind = sind*2
      break if(nind > @ind)
      l = @main[nind]
      nind += 1
      if(nind > @ind)
        ind = nind-1
        if(@main[ind][0] < @main[sind][0])
          @main[sind], @main[ind] = @main[ind], @main[sind]
        end
        break
      end
      r = @main[nind]
      ind = (l[0] > r[0])? nind : nind-1
      if(@main[ind][0] < @main[sind][0])
        @main[sind], @main[ind] = @main[ind], @main[sind]
      end
      sind = ind
    end

    return ans
  end

  def size()
    @ind
  end

  def res()
    @ind = 1
    push([99999999,nil])
  end
end

t = inp[0]
t.times do
  ans = 0
  n = inp[0]
  hp = Hp_neg.new(n+5)
  hp2 = Hp_neg.new(n+5)
  t = na2(n,0)
  t2 = na2(n,0)
  lc = 0
  rc = 0
  n.times do
    k,l,r = inp
    if l-r > 0
      lc += 1
    elsif r-l > 0
      rc += 1
    end
    ans += small(l,r)
    if r-l < 0
      t[k-1].push([r-l])
    elsif r-l > 0
      t2[k-1].push([l-r])
    end
  end
  (lc...n).reverse_each do |i|
    t[i].each do |a|
      hp.push(a)
    end
  end
  (0...lc).reverse_each do |i|
    t[i].each do |a|
      hp.push(a)
    end
    d = hp.pop[0]
    if(d <= 0)
      ans += d * -1
    else
      hp.push([99999999])
    end
  end
  (0...rc).each do |i|
    t2[i].each do |a|
      hp2.push(a)
    end
  end
  (rc...n).each do |i|
    d = hp2.pop[0]
    if(d <= 0)
      ans += d * -1
    else
      hp2.push([9999999])
    end
    t2[i].each do |a|
      hp2.push(a)
    end
  end

  p ans
end
