package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type As [][]int

func (a As) Len() int {
	return len(a)
}

func (a As) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a As) Less(i, j int) bool {
	return a[i][3] > a[j][3]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	for i := 0; i < t; i++ {
		sc.Scan()
		n, _ := strconv.Atoi(sc.Text())
		cs := make(As, 0)
		var lc, rc int
		for j := 0; j < n; j++ {
			sc.Scan()
			k, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			l, _ := strconv.Atoi(sc.Text())
			sc.Scan()
			r, _ := strconv.Atoi(sc.Text())
			cs = append(cs, []int{k, l, r, abs(l - r)})
			if l >= r {
				lc++
			}
			if r > l {
				rc++
			}
		}
		sort.Sort(cs)
		ms := make([]int, n)
		var sum int
		for j := range cs {
			k, l, r := cs[j][0], cs[j][1], cs[j][2]
			var ok bool
			if l >= r {
				for s := min(k-1, lc-1); s >= 0; s-- {
					if ms[s] == 0 {
						sum += l
						ms[s] = 1
						ok = true
						lc--
						break
					}
				}
				if !ok {
					for s := max(k, n-rc-1); s < n; s++ {
						if ms[s] == 0 {
							sum += r
							ms[s] = 1
							ok = true
							lc--
							break
						}
					}
				}
			} else {
				for s := max(k, n-rc); s < n; s++ {
					if ms[s] == 0 {
						sum += r
						ms[s] = 1
						ok = true
						rc--
						break
					}
				}
				if !ok {
					for s := min(k-1, lc); s >= 0; s-- {
						if ms[s] == 0 {
							sum += l
							ms[s] = 1
							ok = true
							rc--
							break
						}
					}
				}
			}
			if !ok {
				for s := 0; s < n; s++ {
					if ms[s] == 0 {
						if s < k {
							sum += l
						} else {
							sum += r
						}
						if l >= r {
							lc--
						}
						if r > l {
							rc--
						}
						ms[s] = 1
						ok = true
						break
					}
				}
			}
		}
		fmt.Println(sum)
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
