
// 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 int MOD = 1e9 + 7;

template<typename T>
struct SegmentTree {
	
	SegmentTree(int _n) : n(_n) { 
		t.resize(n * 4); 
	}
	
	SegmentTree(vector<T>& a) {
		n = (int)a.size();
		t.resize(4 * n);
		build(a, 1, 1, n);
	}
	
	int n;
	vector<T> t;
	
	void build(vector<T>& a, int v, int tl, int tr) {
	    if (tl == tr) {
	        t[v] = a[tl - 1];
	    } else {
	        int tm = (tl + tr) / 2;
	        build(a, v*2, tl, tm);
	        build(a, v*2+1, tm+1, tr);
	        t[v] = max(t[v*2], t[v*2+1]);
	    }
	}
	
	pii query(int l, int r) {
		assert(l >= 1 && r <= n && l <= r);
		return _query(1, 1, n, l, r);
	}
	
	pii _query(int v, int tl, int tr, int l, int r) {
	    if (l > r) 
	        return {-1, -1};
	    if (l == tl && r == tr) {
	        return {t[v], tl};
	    }
	    int tm = (tl + tr) / 2;
	    return max(_query(v*2, tl, tm, l, min(r, tm))
	              ,_query(v*2+1, tm+1, tr, max(l, tm+1), r));
	}
	
	void update(int pos, T new_val) {
		assert(pos >= 1 && pos <= n);
		_update(1, 1, n, pos, new_val);
	}
	
	void _update(int v, int tl, int tr, int pos, T new_val) {
	    if (tl == tr) {
	        t[v] = new_val;
	    } else {
	        int tm = (tl + tr) / 2;
	        if (pos <= tm)
	            _update(v*2, tl, tm, pos, new_val);
	        else
	            _update(v*2+1, tm+1, tr, pos, new_val);
	        t[v] = max(t[v*2], t[v*2+1]);
	    }
	}
};

template<typename T>
struct SegmentTreeMin {
	
	SegmentTreeMin(int _n) : n(_n) { 
		t.resize(n * 4); 
	}
	
	SegmentTreeMin(vector<T>& a) {
		n = (int)a.size();
		t.resize(4 * n);
		build(a, 1, 1, n);
	}
	
	int n;
	vector<T> t;
	
	pii query(int l, int r) {
		assert(l >= 1 && r <= n && l <= r);
		return _query(1, 1, n, l, r);
	}
	
	pii _query(int v, int tl, int tr, int l, int r) {
	    if (l > r) 
	        return {MOD, MOD};
	    if (l == tl && r == tr) {
	        return {t[v], tl};
	    }
	    int tm = (tl + tr) / 2;
	    return min(_query(v*2, tl, tm, l, min(r, tm))
	              ,_query(v*2+1, tm+1, tr, max(l, tm+1), r));
	}
	
	void update(int pos, T new_val) {
		assert(pos >= 1 && pos <= n);
		_update(1, 1, n, pos, new_val);
	}
	
	void _update(int v, int tl, int tr, int pos, T new_val) {
	    if (tl == tr) {
	        t[v] = new_val;
	    } else {
	        int tm = (tl + tr) / 2;
	        if (pos <= tm)
	            _update(v*2, tl, tm, pos, new_val);
	        else
	            _update(v*2+1, tm+1, tr, pos, new_val);
	        t[v] = min(t[v*2], t[v*2+1]);
	    }
	}
};

// 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;
		SegmentTree<int> st(n);
		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;
				st.update(i, p.st.nd);
			} else {
				pii mx = st.query(1, p.st.nd);
				if (mx.st >= i) {
					ans += p.nd + p.st.st;
					st.update(i, mx.st);
					st.update(mx.nd, p.st.nd);
				} else {
					ans += p.nd;
					st.update(i, p.st.nd);
				}
			}
		}
		// cout << ans << endl;
		SegmentTreeMin<int> st2(n);
		FORD(i, n, ind) {
			if (uje.empty()) break;
			para p = *uje.begin(); uje.erase(uje.begin());
			// cout << i << " " << p.st.st << " " << p.st.nd << endl;
			if (p.st.nd < i) {
				ans += p.nd + p.st.st;
				st2.update(i, p.st.nd);
			} else {
				pii mn = st2.query(i + 1, n);
				// cout << mn.st << " " << mn.nd << endl;
				if (mn.st < i) {
					ans += p.nd + p.st.st;
					st2.update(i, mn.st);
					st2.update(mn.nd, p.st.nd);
				} else {
					ans += p.nd;
					st2.update(i, p.st.nd);
				}
			}
		}
		cout << ans << "\n";
	}
	return 0;
}
 
