#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,greater_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
bool cmp(pair<int, int>x, pair<int, int>y){
	return abs(x.first) > abs(y.first);
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int T;
	cin>>T;
	while(T--){
		int n;
		cin>>n;
		long long sum=0;
		vector<pair<int, int>>v;
		set<int>s;
		for(int i=1;i<=n;i++){
			int k, l, r;
			cin>>k>>l>>r;
			v.push_back({l-r, k});
			sum+=r;
			s.insert(i);
		}
		sort(v.begin(), v.end(), cmp);
		long long ans=0;
		for(auto [x, y]:v){
			if(x<0){
				//right is preferable
				auto it=s.upper_bound(y);
				if(it==s.end()) sum+=x;
				else s.erase(it);
			}
			else{
				auto it=s.upper_bound(y);
				if(it!=s.begin()){
					it--;
					sum+=x;
					s.erase(it);
				}
			}
		}
		cout<<sum<<"\n";
	}
}