#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll solveSingleSide(vector<pair<int, ll>> yeeters) {
	int n = yeeters.size();
	for (auto &p_v : yeeters) p_v.first = min(n, p_v.first);
	vector<vector<ll>> byIntroDate(n);
	for (auto p_v : yeeters) {
		if (p_v.first == 0) continue;
		else byIntroDate[p_v.first - 1].push_back(p_v.second);
	}
	ll value = 0;
	multiset<ll> possible;
	for (int pl = n - 1; pl >= 0; --pl) {
		for (ll k : byIntroDate[pl]) possible.insert(k);
		if (!possible.empty()) {
			ll toTake = *possible.rbegin();
			possible.erase(possible.find(toTake));
			value += toTake;
		}
	}
	return value;
}

ll solve() {
	int n; scanf("%d", &n);
	vector<ll> cutoff(n), kaliwa(n), kanan(n);
	for (int i = 0; i < n; ++i) scanf("%lld %lld %lld", &cutoff[i], &kaliwa[i], &kanan[i]);
	vector<pair<int, ll>> leftBias, rightBias;
	ll ans = 0;
	for (int i = 0; i < n; ++i) {
		ans += min(kaliwa[i], kanan[i]);
		if (kaliwa[i] > kanan[i]) {
			leftBias.emplace_back(cutoff[i], kaliwa[i] - kanan[i]);
		} else {
			rightBias.emplace_back(n - cutoff[i], kanan[i] - kaliwa[i]);
		}
	}
	return ans + solveSingleSide(leftBias) + solveSingleSide(rightBias);
}

int main() {
	int t; scanf("%d", &t);
	while (t--) printf("%lld\n", solve());
	return 0;
}
