#require 'pp'
#require 'set'

t = gets.to_i

t.times do |c|
    n = gets.to_i

    memo = Array.new(n + 1)
    index = 1
    r_index = n
    query = []
    n.times do |i|
        k, l, r = gets.split.map(&:to_i)
        query << [k, l, r]
    end

    queue = query.sort
    while queue.empty?.!
        k, l, r = queue.shift

        #if k <= index 
            if l > r
                memo[index] = [k, l, r]
                index += 1
            else
                memo[r_index] = [k,l,r]
                r_index -= 1
            end
        #end

       # pp index
        #pp r_index
        #gets
    end

    #pp memo

    sum = 0
    memo.each_with_index do |a, i|
        next if i == 0

        sum += (i <= a[0] ? a[1] : a[2])
    end
    puts sum
end
