#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

typedef long long ll;

int main () {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		ll answer = 0;
		vector <pair <ll, ll>> a(n);
		for (int i = 0; i < n; i++) {
			ll K, L, R;
			cin >> K >> L >> R;
			answer += R;
			a[i].first = K;
			a[i].second = L - R;
		}
		sort(a.begin(), a.end());
		multiset <ll> values;
		vector <ll> pre(n);
		ll sum = 0;
		for (int i = 0; i < n; i++) {
			if (a[i].second < 0) {
				pre[i] = sum;
				continue;
			}
			values.insert(a[i].second);
			sum += a[i].second;
			while (values.size() > a[i].first) {
				ll x = *values.begin();
				sum -= x;
				values.erase(values.begin());
			}
			pre[i] = sum;
		}
		vector <ll> suf(n);
		sum = 0;
		while (!values.empty())
			values.erase(values.begin());
		for (int i = n - 1; i >= 0; i--) {
			if (a[i].second >= 0) {
				suf[i] = sum;
				continue;
			}
			values.insert(a[i].second);
			while (values.size() > n - a[i].first) {
				ll x = *values.rbegin();
				sum += x;
				values.erase(values.find(x));
			}
			suf[i] = sum;
		}
		cout << answer + suf[0] + pre[n - 1] << '\n';
	}
	return 0;
}