#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int t, n, k, a, b;
vector<int> vec[2][200009];
long long ans;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> t;
	while (t--) {
		cin >> n;
		ans = 0;
		for (int i = 1; i <= n; i++) {
			vec[0][i].clear();
			vec[1][i].clear();
		}
		for (int i = 1; i <= n; i++) {
			cin >> k >> a >> b;
			if (a < b) {
				vec[1][k + 1].pb(b - a);
			}
			else if (a > b) {
				vec[0][k].pb(a - b);
			}
			ans += min(a, b);
		}
		int cnt = 0;
		multiset<int> w;
		for (int i = n; i > 0; i--) {
			//cnt++;
			//sort(vec[0][i].begin(), vec[0][i].end());
			//reverse(vec[0][i].begin(), vec[0][i].end());
			int sz = vec[0][i].size();
			for (int j = 0; j < sz; j++) {
				w.insert(vec[0][i][j]);
			}
			if (w.empty()) continue;
			auto p = w.end();
			p--;
			ans += *p;
			w.erase(p);
		}
		w.clear();
		for (int i = 0; i <= n; i++) {
			int sz = vec[1][i].size();
			for (int j = 0; j < sz; j++) {
				w.insert(vec[1][i][j]);
			}
			if (w.empty()) continue;
			auto p = w.end();
			p--;
			ans += *p;
			w.erase(p);
		}
		cout << ans << E;
	}
	//system("pause");
	return 0;
}