#[allow(unused_imports)]
use {
   proconio::{fastout, input, marker::*},
   std::cmp::*,
   std::collections::*,
};

#[fastout]
fn main() {
   input! {
      t: usize,
   }
   for _ in 0..t {
      input! {
         n: usize,
         klr: [(Usize1, usize, usize); n]
      }
      if n == 1 {
         let (_, l, _) = klr[0];
         println!("{}", l);
      }
      let mut ans = 0usize;
      let mut map1 = vec![vec![]; n];
      let mut map2 = vec![vec![]; n];
      for &(k, l, r) in klr.iter() {
         if l == r || k == n - 1 {
            ans += l;
         } else if l > r {
            ans += r;
            map1[k].push(l - r);
         } else {
            ans += l;
            map2[k].push(r - l);
         }
      }
      let mut q1 = BinaryHeap::new();
      let mut q2 = BinaryHeap::new();
      for i in 0..n {
         if map1[i].is_empty() {
            continue;
         }
         for &x in map1[i].iter() {
            q1.push(Reverse(x));
         }
         while q1.len() > i + 1 {
            q1.pop().unwrap();
         }
      }
      for i in (0..n - 1).rev() {
         if map2[i].is_empty() {
            continue;
         }
         for &x in map2[i].iter() {
            q2.push(Reverse(x));
         }
         while q2.len() > n - i - 1 {
            q2.pop().unwrap();
         }
      }
      for Reverse(x) in q1.iter() {
         ans += x;
      }
      for Reverse(x) in q2.iter() {
         ans += x;
      }
      println!("{}", ans);
   }
}
