T = gets.to_i
results = Array.new T
T.times do |i|
    n = gets.to_i
    score = 0
    l_list = []
    r_list = []
    n.times do |i|
        k, l, r = gets.split.map(&:to_i)
        if l == r || k == n
            score += l
        elsif l > r
            score += r
            l_list.push([k, l-r])
        else
            score += l
            r_list.push([n-k, r-l])
        end
    end
    [l_list,r_list].each do |list|
        list = list.sort_by{|x| x[0]}
        mure = []
        sum = 0
        size = 0
        before_k = 0
        list.each do |k, v|
            if before_k > 0 && k != before_k
                mure = mure.sort[0..before_k]
            end
            mure.push(v)
            before_k = k
        end
        mure = mure.sort[0..before_k]
        score += mure.sum
    end
    results[i] = score
end

puts results.join("\n")