import java.util.PriorityQueue

fun solve() {
    val n = readLine()!!.toInt()
    var k = MutableList(n,{_->0})    
    var l = MutableList<Long>(n,{_->0})    
    var r = MutableList<Long>(n,{_->0})    
    for(i in 0 until n){
        val (ktmp,ltmp,rtmp) = readLine()!!.split(" ").map(String::toInt)
        k[i] = ktmp-1
        l[i] = ltmp.toLong()
        r[i] = rtmp.toLong()
    }

    var lvec = MutableList(n,{_->mutableListOf<Long>()})
    var rvec = MutableList(n,{_->mutableListOf<Long>()})
    var ans = 0L
    for(i in 0 until n) {
        if(l[i]<r[i]){
            if(n-2-k[i]>=0) rvec[n-2-k[i]].add(r[i]-l[i])
            ans+=l[i]
        }
        else{
            lvec[k[i]].add(l[i]-r[i])
            ans+=r[i]
        }
    }

    val calc = {vec:MutableList<MutableList<Long>> ->
        val comp = compareBy<Long>({-it})
        var que = PriorityQueue<Long>(comp)
        var res = 0L
        for(i in n-1 downTo 0){
            for(elem in vec[i]) que.add(elem)
            if(!que.isEmpty()){
                val v = que.poll()
                res+=v
            }   
        }
        res
    }
    ans+=calc(lvec)
    ans+=calc(rvec)
    println(ans)
}
fun main() {
    val t = readLine()!!.toInt()
    for(i in 0 until t){
        solve()
    }
}
