#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main(){
	int t; cin >> t;
	while(t--){
		int n; cin >> n;
		vector<P> ls(0), rs(0);
		int k, l, r; ll ans=0;
		REP(i,n){
			cin >> k >> l >> r;
			ans+=max(l,r);
			if(l>r)
				ls.push_back({k,abs(l-r)});
			else
				rs.push_back({n-k,abs(l-r)});
		}
		sort(ALL(ls));
		sort(ALL(rs));
		multiset<int> sel;
		REP(i,ls.size()){
			if(ls[i].first<=sel.size()){
				if(*sel.begin()<ls[i].second){
					ans-=*sel.begin();
					sel.erase(sel.begin());
					sel.insert(ls[i].second);
				}
				else{
					ans-=ls[i].second;
				}
			}
			else{
				sel.insert(ls[i].second);
			}
		}
		multiset<int> ser;
		REP(i,rs.size()){
			if(rs[i].first<=ser.size()){
				if(rs[i].first==0){
					ans-=rs[i].second;
					continue;
				}
				if(*ser.begin()<rs[i].second){
					ans-=*ser.begin();
					ser.erase(ser.begin());
					ser.insert(rs[i].second);
				}
				else{
					ans-=rs[i].second;
				}
			}
			else{
				ser.insert(rs[i].second);
			}
		}
		cout << ans << endl;
	}
	return 0;
}