#include <bits/stdc++.h>

#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long  ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9 + 7;
void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);}

ll cal(vector<pii>& v){
	ll rtn =0;
	priority_queue<int> pq;
	int m = sz(v);
	vector<VI> kh(m+2);
	for(auto x: v) kh[min(x.first, m)].push_back(x.second);
	
	repr(i, m, 1) {
		for(auto w: kh[i]) pq.emplace(w);
		if(sz(pq)){
			rtn += pq.top();
			pq.pop();
		}
	}
	return rtn;
}

void solove(){
	int n; cin >>n;
	vector<pii> lf, rg;
	ll ans = 0;
	rep(i, n){
		int k, l, r;
		cin >>k >>l >>r;
		if(l >r){
			lf.emplace_back(k, l-r);
		}else if(l < r){
			rg.emplace_back(n-k, r-l);
		}
		ans += min(l, r);
	}
	
	ans += cal(lf) + cal(rg);
	cout << ans <<"\n";
}

int main()
{
	cin.tie(0); ios_base::sync_with_stdio(false);
	int t;
	cin >>t;
	rep(_g, t) solove();
	return 0;
}
