T = gets.to_i

T.times do
    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
        sorted = false
        list.each do |x|
            if size < x[0]
                sum += x[1]
                size += 1
                mure.push(x[1])
            else
                unless sorted 
                    mure = mure.sort
                    sorted = true
                end
                if mure[0] < x[1]
                    sum += x[1] - mure[0]
                    mure[0] = x[1]
                    sorted = false
                end
            end
        end
        score += sum
    end
    puts score
end