#include <bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T; cin >> T; 
	int inf = 1145141919; 
	while(T--){
		int N; cin >> N; 
		long long ans = 0; 
		vector<pair<long long, int>> left, right; 
		for(int i = 0; i < N; i++){
			int K; long long L, R; 
			cin >> K >> L >> R; 
			ans += min(L, R); 
			if(L >= R) left.emplace_back(L - R, K); 
			else right.emplace_back(R - L, N - K); 
		}		
		sort(left.begin(), left.end(), greater<pair<long long, int>>()); 
		sort(right.begin(), right.end(), greater<pair<long long, int>>()); 
		int a = left.size(); int b = N - a; 
		set<int> A, B; 
		for(int i = 1; i <= a; i++) A.insert(i); 
		for(int i = 1; i <= b; i++) B.insert(i); 
		A.insert(-inf); A.insert(inf); 
		B.insert(-inf); B.insert(inf); 
		for(auto p : left){
			auto it = A.upper_bound(p.second); 
			it--; 
			if(*it == -inf) continue;
			ans += p.first; 
			A.erase(it); 			
		}
		for(auto p : right){
			auto it = B.upper_bound(p.second); 
			it--; 
			if(*it == -inf) continue;
			ans += p.first; 
			B.erase(it); 			
		}
		cout << ans << "\n"; 
	}
}