"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(){return +cin[cid++];}
function nextstr(){return cin[cid++];}
function nextbig(){return BigInt(cin[cid++]);}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}

function segTree(n){
  var l = n.length, u = l-1, x = 2, i, v;
  while(u)u >>= 1, x <<= 1;
  v = this.seg = Array(x-1).fill(0);
  this.l = x = (x >> 1) - 1;
  for(i = 0; i < l; i++)v[i+x] = n[i];
  for(i = x-1; i >= 0; i--)v[i] = segSolve(v[i+i+1],v[i+1<<1]);
}
function segSolve(a,b){
  return Math.max(a,b);
}
segTree.prototype={
  get:function(l,r){//[l,r)
    if(l >= r)return 0;
    var v = this.seg;
    return q(l,r,0,this.l+1,0);
    function q(l,r,x,y,i){
      if(l === x && r === y)return v[i];
      var m = x+y >> 1;
      if(m < r)if(l < m)return segSolve(q(l,m,x,m,i+i+1),q(m,r,m,y,i+1<<1));
      else return q(l,r,m,y,i+1<<1);
      return q(l,r,x,m,i+i+1);
    }
  },
  update:function(x,i){
    var v = this.seg;
    i += this.l;
    v[i] = x;
    for(i = i-1>>1; i >= 0; i = i-1>>1){
      v[i] = segSolve(v[i+i+1],v[i+1<<1]);
    }
  },
}

var myOut = main();
if(myOut !== undefined)console.log(String(myOut));

function main(){
  var q = next();
  var ans = xArray(0,q);
  var inf = 1e9;
  var lseg = new segTree(Array(2e5+1).fill().map((a,i)=>i));
  var rseg = new segTree(Array(2e5+1).fill().map((a,i)=>i));
  for(var i = 0; i < q; i++){
    var n = next();
    var l = [], r = [];
    for(var j = 0; j < n; j++){
      var [k,ll,rr] = nexts(3);
      if(ll === rr || k === n)ans[i] += ll;
      else if(ll > rr){
        ll -= rr;
        ans[i] += rr;
        l.push([k,ll]);
      }else{
        rr -= ll;
        ans[i] += ll;
        r.push([n-k,rr]);
      }
    }
    l.sort((a,b)=>b[1]-a[1]);
    r.sort((a,b)=>b[1]-a[1]);
    var lset = new Set(), rset = new Set();
    for(var j = 0; j < l.length; j++){
      var [u,v] = l[j];
      var id = lseg.get(0,u);
      if(id >= 0){
        lset.add(id);
        lseg.update(-1,id);
        ans[i] += v;
      }
    }
    for(var j = 0; j < r.length; j++){
      var [u,v] = r[j];
      var id = rseg.get(0,u);
      if(id >= 0){
        rset.add(id);
        rseg.update(-1,id);
        ans[i] += v;
      }
    }
    for(var j of lset)lseg.update(j,j);
    for(var j of rset)rseg.update(j,j);
  }
  return ans.join("\n");
}