#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll solve(vector<pair<int, int>> &a) {
	int n = a.size();
	vector<vector<int>> events(n + 1);
	for (auto [when, x] : a) {
		events[min(when, n)].push_back(x);
	}
	priority_queue<int> pq;
	ll res = 0;
	for (int i = n; i; i--) {
		for (auto el : events[i]) {
			pq.push(el);
		}
		if (pq.size()) {
			res += pq.top();
			pq.pop();
		}
	}
	return res;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		array<vector<pair<int, int>>, 2> want;
		ll ans = 0;
		for (int i = 0; i < n; i++) {
			int t, L, R;
			cin >> t >> L >> R;
			ans += min(L, R);
			want[L >= R].emplace_back(L >= R ? t : n - t, max(L, R) - min(L, R));
		}
		cout << ans + solve(want[0]) + solve(want[1]) << '\n';
	}
}