#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mk make_pair
const int N=200010;
int n, m;
vector<pair<int, pair<int, int>> > v[N], v1[N];
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d",&n);
		int k, l, r;
		ll ans=0;
		for(int i=0; i<=n+1; ++i){
			v[i].clear();
			v1[i].clear();
		}
		multiset<pair<int, pair<int, int>>>s;
		for(int i=0; i<n; ++i){
			scanf("%d%d%d",&k,&l,&r);
			if(r>=l){
				v1[k+1].push_back(mk(-(r-l), mk(-r, -l)));
			}
			else{
				v[k].push_back(mk(-(l-r), mk(-l, -r)));
			}
		}
		for(int i=n; i>=1; --i){
			for(auto it:v[i])s.insert(it);
				if(s.size()>0){
					ans-=s.begin()->second.first;
					s.erase(s.begin());
				}
		}
		while(s.size()>0){
				ans-=s.begin()->second.second;
				s.erase(s.begin());
		}
		for(int i=1; i<=n; ++i){
			for(auto it:v1[i])s.insert(it);
				if(s.size()>0){
					ans-=s.begin()->second.first;
					s.erase(s.begin());
				}
		}
		while(s.size()>0){
				ans-=s.begin()->second.second;
				s.erase(s.begin());
		}
		for(auto it:v1[n+1])ans-=it.second.second;
		printf("%lld\n",ans);
	}
}
