#include <bits/stdc++.h>
#define int long long

using namespace std;

vector<pair<int, int> > l1;
priority_queue<int, vector<int>, greater<int> > q1;
vector<pair<int, int> > l2;
priority_queue<int, vector<int>, greater<int> > q2;

signed main() {
	int t, n, k, l, r, s1, s2;
	cin >> t;
	while(t--) {
		s1 = 0;
		s2 = 0;
		int n;
		cin >> n;
		for(int i = 0; i < n; i++) {
			cin >> k >> l >> r;
			if(l < r) {
				l2.push_back({n-k, r-l});
			}
			else l1.push_back({k, l-r});
			s1 += min(l, r);
		}
		sort(l1.begin(), l1.end());
		sort(l2.begin(), l2.end());
		for(int i = 0; i < l1.size(); i++) {
			if(l1[i].first == 0) continue;
			if(q1.size() < l1[i].first) q1.push(l1[i].second);
			else if(q1.size() == l1[i].first && q1.top() < l1[i].second) {
				q1.pop();
				q1.push(l1[i].second);
			}
		}
		while(q1.size()) {
			s1 += q1.top();
			q1.pop();
		}
		for(int i = 0; i < l2.size(); i++) {
			if(l2[i].first == 0) continue;
			if(q2.size() < l2[i].first) q2.push(l2[i].second);
			else if(q2.size() == l2[i].first && q2.top() < l2[i].second) {
				q2.pop();
				q2.push(l2[i].second);
			}
		}
		while(q2.size()) {
			s1 += q2.top();
			q2.pop();
		}
		cout << s1 << '\n';
		vector<pair<int, int> >().swap(l1);
		vector<pair<int, int> >().swap(l2);
	}
}