use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn main() {
    input! {
        t: usize,
    }
    for _ in 0..t {
        input! {
            n: usize,
            klr: [(Usize1, isize, isize); n],
        }
        let mut left = BinaryHeap::new();
        let mut right = BinaryHeap::new();
        let mut s = 0;
        for i in 0..n {
            let (ki, li, ri) = klr[i];
            if li > ri {
                left.push((Reverse(ki), li - ri, i));
            } else if li < ri {
                right.push((ki, ri - li, i));
            } else {
                s += li;
            }
        }
        for t in 0..n {
            if let Some((Reverse(ki), _, i)) = left.pop() {
                if ki >= t {
                    s += klr[i].1;
                } else {
                    s += klr[i].2;
                }
            }
        }
        for t in (0..n).rev() {
            if let Some((ki, _, i)) = right.pop() {
                if ki < t {
                    s += klr[i].2;
                } else {
                    s += klr[i].1;
                }
            }
        }
        println!("{}", s);
    }
}
