#![allow(unused_imports)]
#![allow(unused_macros)]
use num::{Bounded, One, Zero};
use std::cmp::{max, min};
use std::collections::*;
use std::io::{stdin, Read};
use std::ops::{Add, Mul};
pub trait Monoid {
    fn unit() -> Self;
    fn add(lhs: &Self, rhs: &Self) -> Self;
}

#[derive(Copy, Clone, Debug)]
pub struct Sum<T>(T);

impl<T> Monoid for Sum<T>
where
    T: Zero + Add<Output = T> + Copy,
{
    fn unit() -> Self {
        Self(T::zero())
    }
    fn add(lhs: &Self, rhs: &Self) -> Self {
        Self(lhs.0 + rhs.0)
    }
}

impl<T> From<T> for Sum<T> {
    fn from(x: T) -> Self {
        Self(x)
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Product<T>(pub T);

impl<T: Copy + One + Mul<Output = T>> Monoid for Product<T> {
    fn unit() -> Self {
        Self(T::one())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0 * r.0)
    }
}

impl<T> From<T> for Product<T> {
    fn from(v: T) -> Self {
        Product(v)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Max<T>(pub T);

impl<T: Copy + Ord + Bounded> Monoid for Max<T> {
    fn unit() -> Self {
        Self(<T as Bounded>::min_value())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0.max(r.0))
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Min<T>(pub T);

impl<T: Copy + Ord + Bounded> Monoid for Min<T> {
    fn unit() -> Self {
        Self(<T as Bounded>::max_value())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0.min(r.0))
    }
}

pub struct SegTree<T: Monoid + Clone> {
    dat: Vec<T>,
    n: usize,
}

impl<T: Monoid + Copy> SegTree<T> {
    pub fn new(n: usize) -> Self {
        let dat = vec![T::unit(); n << 1];
        SegTree { dat: dat, n: n }
    }
    pub fn update(&mut self, k: usize, v: T) {
        let mut k = k + self.n;
        self.dat[k] = v;
        while {
            k >>= 1;
            k > 0
        } {
            self.dat[k] = T::add(&self.dat[k << 1], &self.dat[k << 1 | 1]);
        }
    }
    pub fn get(&self, a: usize, b: usize) -> T {
        let mut a = a + self.n;
        let mut b = b + self.n;
        let mut va = T::unit();
        let mut vb = T::unit();
        while a < b {
            if a & 1 != 0 {
                va = T::add(&va, &self.dat[a]);
                a += 1;
            }
            if b & 1 != 0 {
                vb = T::add(&self.dat[b - 1], &vb);
            }
            a >>= 1;
            b >>= 1;
        }
        T::add(&va, &vb)
    }
}

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

#[cfg(debug_assertions)]
macro_rules! debug {
    ($( $args:expr ),*) => { eprintln!( $( $args ),* ); }
}

#[cfg(not(debug_assertions))]
macro_rules! debug {
    ($( $args:expr ),*) => {
        ()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, t: usize);
    for _ in 0..t {
        parse!(it, n: usize, klr: [(usize, i64, i64); n]);
        let klr: Vec<(usize, i64, i64)> = klr;
        let mut ak: Vec<_> = klr.iter().map(|&(k, l, r)| (l - r, k)).collect();
        ak.sort();
        ak.reverse();
        let mut seg = SegTree::<Sum<usize>>::new(n);
        let mut ret: i64 = klr.iter().map(|x| x.2).sum();
        for &(a, k) in &ak {
            if a <= 0 {
                break;
            }
            let b = seg.get(0, k).0;
            if b < k {
                let tmp = seg.get(k - 1, k).0;
                seg.update(k - 1, Sum(tmp + 1));
                ret += a;
            }
        }
        ak.reverse();
        let mut seg = SegTree::<Sum<usize>>::new(n);
        for &(a, k) in &ak {
            if a >= 0 {
                break;
            }
            if k == n {
                ret += a;
                continue;
            }
            let b = seg.get(k, n).0;
            if b < n - k {
                let tmp = seg.get(k, k + 1).0;
                seg.update(k, Sum(tmp + 1));
            } else {
                ret += a;
            }
        }
        println!("{}", ret);
    }
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}
