#include <bits/stdc++.h>

using namespace std;

int t, n;
vector<pair<int, int> > a, b;
int l, r, k;
long long ans;

std::priority_queue<int, vector<int>, greater<int> > q;

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		a.clear(), b.clear(), ans = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d%d%d", &k, &l, &r);
			if(l > r){
				ans += r;
				a.push_back({k, l - r});
			}else if(l < r && n - k){
				ans += l;
				b.push_back({n - k, r - l});
			}else{
				ans += l;
			}
		}
		sort(a.begin(), a.end());
		for(int i = 0;i < a.size();i++){
			if(q.size() < a[i].first){
				q.push(a[i].second);
			}else{
				if(q.top() < a[i].second){
					q.pop();
					q.push(a[i].second);
				}
			}
		}
		while(!q.empty()) ans += q.top(), q.pop();
		swap(a, b);
		sort(a.begin(), a.end());
		for(int i = 0;i < a.size();i++){
			if(q.size() < a[i].first){
				q.push(a[i].second);
			}else{
				if(q.top() < a[i].second){
					q.pop();
					q.push(a[i].second);
				}
			}
		}
		while(!q.empty()) ans += q.top(), q.pop();
		printf("%lld\n", ans);
	}
}