#include <bits/stdc++.h>
using namespace std;

using VI = vector <int>;
using ll = long long;

ll solve(int n, VI pos, VI L, VI R) {
	vector <vector <int> > V(n+1);
	for (int i = 0; i < n; i ++) {
		int val = R[i] - L[i];
		V[pos[i]].push_back(val);
	}
	ll ans = accumulate(L.begin(), L.end(), 0ll);
	priority_queue <int> pq;
	for (int i = 0; i <= n; i ++) {
		for (auto x : V[i]) pq.push(x);
		while (pq.size() > i) {
			ans += pq.top();
			pq.pop();
		}
	}
	return ans;
}

int main() {
	cin.tie(0); ios::sync_with_stdio(0); cout.tie(0);
	int T, n;
	cin >> T;
	while (T --) {
		cin >> n;
		VI K(n+1), L(n+1), R(n+1);
		VI vl, vr;
		for (int i = 1; i <= n; i ++) {
			cin >> K[i] >> L[i] >> R[i];
			if (L[i] >= R[i]) vl.push_back(i);
			else vr.push_back(i);
		}
		ll ans = 0;
		vector <int> pos, l, r;
		int sz = vl.size();
		for (auto x : vl) {
			pos.push_back(min(sz, K[x]));
			l.push_back(L[x]);
			r.push_back(R[x]);
		}
		ans += solve(sz, pos, l, r);
		pos.clear(), l.clear(), r.clear();
		sz = vr.size();
		for (auto x : vr) {
			pos.push_back(min(sz, n - K[x]));
			l.push_back(R[x]);
			r.push_back(L[x]);
		}
		ans += solve(sz, pos, l, r);
		cout << ans << endl;
	}
	return 0;
}
