package main
import (
    "fmt"
    "os"
    "sort"
    "container/heap"
)
type kek struct{
    k int
    l int64
}
type IntHeap []int64

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
    *h = append(*h, x.(int64))
}
func (h *IntHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[n-1]
    *h = old[0 : n-1]
    return x
}
func solve(a []kek) int64 {
    var ans int64 = 0
    sort.Slice(a, func(i, j int) bool {
        return a[i].k < a[j].k
    })
    var na = len(a)
    h := &IntHeap{}
    heap.Init(h)
    for i := 0; i < na; i++ {
        heap.Push(h, a[i].l)
        ans += int64(a[i].l)
        if h.Len() > a[i].k{
            ans -= heap.Pop(h).(int64)
        }
    }
    return ans
}
func main() {
    var t,n int
    fmt.Fscan(os.Stdin, &t)
    for ;t>0;t-- {
        fmt.Fscan(os.Stdin, &n)
        var a []kek
        var b []kek
        var ans int64 = 0
        for i := 0; i < n; i++ {
            var k int
            var l,r int64
            fmt.Fscan(os.Stdin,&k,&l,&r)
            if l>=r{
                ans += r
                a=append(a,kek{k,l-r})
            } else {
                ans += l
                b=append(b,kek{n-k,r-l})
            }
        }
        ans += solve(a)
        ans += solve(b)
        fmt.Println(ans)
    }
}