#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
#define ff first
#define ss second
#define endl '\n'
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#include "string"

struct Camel {
	int k, l, r;
	bool operator < ( Camel const & tmp) { 
		if(k == tmp.k) 
			return max(l,r) > max(tmp.l, tmp.r);
		return k < tmp.k;
	}
};

int32_t main(){
	boost;
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		Camel a[n];
		vector < Camel > l, r;
		for(int i = 0; i < n; i++) { 
			cin >> a[i].k >> a[i].l >> a[i].r;
			if(a[i].l > a[i].r)
				l.push_back(a[i]);
			else
				r.push_back(a[i]);
		}
		sort(l.begin(), l.end());
		multiset <pi> taken;
		int ans = 0;
		for(int i = 0; i < l.size(); i++) {
			if(taken.size() >= l[i].k) {
				pi top = *taken.begin();
				if(top.ff < l[i].l) {
					taken.erase(taken.find(top));
					ans -= top.ff;
					ans -= top.ss;
					ans += l[i].l;
					taken.insert({l[i].l,-l[i].r});
				}
				else 
					ans += l[i].r;
			}
			else {
				ans += l[i].l;
				taken.insert({l[i].l,-l[i].r});
			}
		}

		taken.clear();
		sort(r.rbegin(),r.rend());

		for(int i = 0 ; i < r.size(); i++) {
			int nk = n - r[i].k;
			if(taken.size() >= nk) {
				pi top = *taken.begin();
				if(top.ff < r[i].r) {
					taken.erase(taken.find(top));
					ans -= top.ff;
					ans -= top.ss;
					ans += r[i].r;
					taken.insert({r[i].r,-r[i].l});
				}
				else
					ans += r[i].l;
			}
			else {
				ans += r[i].r;
				taken.insert({r[i].r,-r[i].l});
			}
		}

		cout << ans << endl;
	}
}

