
// Problem : E - Camel Train
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;	

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

// sprawdz MODULO!
int main() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	REP(_, t) {
		int n; cin >> n;
		ll ans = 0;
		multiset<para, greater<para>> dod, uje;
		REP(i, n) {
			int k, l, r;
			cin >> k >> l >> r;
			if (l == r || k == n) {
				ans += l;
			} else {
				if (l > r) {
					dod.insert({{l - r, k}, r});
				} else {
					uje.insert({{r - l, k}, l});
				}
			}
		}
		int ind = 0;
		int mx = 0;
		RI(i, n) {
			if (dod.empty()) {
				ind = i;
				break;
			}
			para p = *dod.begin(); dod.erase(dod.begin());
			if (p.st.nd >= i) {
				ans += p.nd + p.st.st;
			} else {
				if (mx >= i) {
					ans += p.nd + p.st.st;
				} else {
					ans += p.nd;
				}
			}
			mx = max(mx, p.st.nd);
		}
		// cout << ans << " ";
		int mn = n + 1;
		FORD(i, n, ind) {
			if (uje.empty()) break;
			para p = *uje.begin(); uje.erase(uje.begin());
			if (p.st.nd < i) {
				ans += p.nd + p.st.st;
			} else {
				if (mn < i) {
					ans += p.nd + p.st.st;
				} else {
					ans += p.nd;
				}
			}
			mn = min(mn, p.st.nd);
			// cout << ans << " ";
		}
		cout << ans << "\n";
	}
	return 0;
}
 
