#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (long long  i=a;i<n;i++)
#define sz(A) (int)(A.size())
#define all(x) (x).begin(),(x).end()
#define get(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
//int k[200010], l[200010], r[200010];
void solve() {
	int n;
	cin >> n;
	map<int, vector<PII> > ma;
	rep(i, 0, n) {
		//cin >> k[i] >> l[i] >> r[i];
		int k, l, r;
		cin >> k >> l >> r;
		ma[k].push_back({ l,r });
	}
	priority_queue<pair<int, int> > pq;
	priority_queue<pair<int, int> > pq2;
	auto it = ma.end();
	it--;
	ll res = 0;
	int pos = -1;
	while (true) {
		vector<PII> vp = (*it).second;
		if (pos == -1) {
			pos = (*it).first;
		}
		rep(i, 0, sz(vp)) {
			if (vp[i].first > vp[i].second) {
				pq.push(vp[i]);
			}
			else {
				pq2.push(vp[i]);
			}
		}
		res += (pq.top().first);
		pq.pop();
		pos--;
		if (pos == -1)break;
		if (it == ma.begin())break;
		it--;
	}
	if (pos == -1) {
		while (true) {
			vector<PII> vp = (*it).second;
			rep(i, 0, sz(vp)) {
				res += vp[i].second;
			}
			if (it == ma.begin())break;
			it--;
		}
	}
	else {
		while (pos != -1) {
			res += pq2.top().first;
			pos--;
		}
	}
	
	while (!pq.empty()) {
		res += pq.top.second;
		pq.pop();
	}
	while (!pq2.empty()) {
		res += pq2.top.second;
		pq2.pop();
	}
	cout << res;
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T_ = 1;
	cin >> T_;
	for (int i = 0; i < T_; i++)solve();
	return 0;
}





