#![allow(non_snake_case,unused_imports,dead_code)]
use std::str::FromStr;
use std::io::Read;
use std::collections::{HashMap,HashSet,VecDeque,BinaryHeap};
use std::cmp::{max,min,Reverse,Ordering};
use std::time::{Duration, Instant};
use rand::Rng;

use proconio::input;
use proconio::marker::{Chars,Usize1,Isize1};

fn main() {

    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut buf_it = buf.split_whitespace();

    let T = buf_it.next().unwrap().parse::<usize>().unwrap();

    let INF = 1e9 as isize +10;
    for _ in 0..T{
        let N = buf_it.next().unwrap().parse::<usize>().unwrap();
        let KLR = (0..N).map(|_|
            (buf_it.next().unwrap().parse::<usize>().unwrap(),
            buf_it.next().unwrap().parse::<isize>().unwrap(),
            buf_it.next().unwrap().parse::<isize>().unwrap())
        ).collect::<Vec<_>>();
        let mut ans = 0;

        let mut KdLR = Vec::new();
        for (k,l,r) in KLR{
            KdLR.push((r-l,k,l,r));
        }
        KdLR.sort_by_key(|x| -(x.0.abs()));
        let mut TR = SegmentTree::new(N,min,-1isize);
        let mut TL = SegmentTree::new(N,max,INF);

        for i in 0..N{
            TR.set(i,i as isize);
            TL.set(i,i as isize);
        }

        for (dlr,k,L,R) in KdLR{
            if dlr>0{
                let r = TR.get(k,N);
                if r==INF || r==-1{
                    ans += L;
                    continue;
                }
                ans += R;
                TR.set(r as usize,INF);
            }
            else{
                let l = TL.get(0,k);
                if l == -1{
                    ans += R;
                    continue;
                }
                ans += L;
                TL.set(l as usize,-1);
            }
        }

        println!("{}",ans);
    }

}

struct SegmentTree<N,F>
where F:Fn(N,N) ->N{
    n: usize,
    T: Vec<N>,
    func: F,
    identity: N,
}

impl<N:Copy,F> SegmentTree<N,F>
where F:Fn(N,N) ->N {
    fn new(n:usize,func:F,identity:N) -> SegmentTree<N,F>{
        let mut T = SegmentTree { n:n,T:Vec::new(),func:func,identity:identity};
        T.init();
        T
    }
    fn init(&mut self){
        self.n = 1<<format!("{:b}",self.n-1).len();
        self.T = (0..(2*self.n-1)).map(|_| self.identity).collect();
    }
    fn set(&mut self,index:usize,value:N){
        let i = index + self.n - 1;
        self.T[i] = value;
        self._update_parent((i-1)/2);
    }
    fn _update_parent(&mut self,i:usize){
        self.T[i] = (self.func)(self.T[i*2+1],self.T[i*2+2]);
        if i>0 { self._update_parent((i-1)/2)}
    }
    fn get(&self,l:usize,r:usize) -> N{
    self._get(l,r,0,0,self.n)
    }
    fn _get(&self,l:usize,r:usize,i:usize,a:usize,b:usize) -> N{
        if l<=a && b<=r {
            self.T[i]
        } else if b<=l || r<=a {
            self.identity
        } else{
            (self.func)(self._get(l,r,2*i+1,a,(a+b)/2),
                        self._get(l,r,2*i+2,(a+b)/2,b))
        }
    }
}