#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#include <iomanip>
#include <unordered_map>
#include <stack>
#include <cstring>
#include <cctype>
#include <random>

using namespace std;
typedef long long  ll;
const ll INF = 1000000007;
const ll mod = 998244353;

ll f(vector<pair<ll, ll>> p) {
	int n = p.size();
	for (ll i = 0; i < n; i++)
	{
		p[i].first = min(p[i].first, n);
	}
	vector<vector<int>> s(n + 1);
	for (ll i = 0; i < n; i++)
	{
		s[p[i].first].push_back(p[i].second);
	}
	ll res = 0;
	priority_queue<int> q;
	for (int i = n; i >= 1; --i) {
		for (int x : s[i]) q.push(x);
		if (q.size() > 0) {
			res += q.top();
			q.pop();
		}
	}
	return res;
}

void solve() {
	ll n;
	cin >> n;
	vector<pair<ll, ll>> pl, pr;
	ll ans = 0;
	for (ll i = 0; i < n; i++)
	{
		ll k, l, r;
		cin >> k >> l >> r;
		ll ans = 0;
		ll m = min(l, r);
		ans += m;
		l -= m; r -= m;
		if (l > 0) pl.emplace_back(k, l);
		else pr.emplace_back(n - k, r);
	}
	ans += f(pl);
	ans += f(pr);
	cout << ans << endl;
}



int main() {
	int ts;
	cin >> ts;
	for (ll i = 0; i < ts; i++)
	{
		solve();
	}
	return 0;
}