/*input
3
2
1 5 10
2 15 5
3
2 93 78
1 71 59
3 57 96
19
19 23 16
5 90 13
12 85 70
19 67 78
12 16 60
18 48 28
5 4 24
12 97 97
4 57 87
19 91 74
18 100 76
7 86 46
9 100 57
3 76 73
6 84 93
1 6 84
11 75 94
19 15 3
12 11 34
*/

// Nihesh Anderson - knandy

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
ll MOD=1000000007;

#define endl '\n'
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
ll INF = 2000000000000000001;
long double EPS = 1e-9;
ll power(ll a, ll n, ll md){if(n==0){return 1;}else{ll res=power(a,n/2,md);res=(res*res)%md;if(n%2!=0){res=(res*a)%md;}return res;}}
random_device rndm;
mt19937 grndm(rndm());
void mix(ll* a, ll* b){shuffle(a,b,grndm);}

int main(){
	fast_cin();
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
	ll t; cin >> t;
	while(t--) {
		ll n; cin >> n;
		vector<pair<pair<ll, ll>, ll> > v(n);
		vector<ll> assign(n + 1);
		for(ll i = 0; i < n; i++) {
			cin >> v[i].second >> v[i].first.first >> v[i].first.second;
		}
		sort(v.begin(), v.end(), [&](pair<pair<ll, ll>, ll> x, pair<pair<ll, ll>, ll> y) -> bool {
			return abs(x.first.first - x.first.second) > abs(y.first.first - y.first.second);
		});
		set<ll> rem;
		for(ll i = 1; i <= n; i++) {
			rem.insert(i);
		}
		ll happy = 0;
		ll lshift = n + 1, rshift = 0;
		for(ll i = 0; i < n; i++) {
			// cout << lshift << " " << rshift << endl;
			// cout << v[i].first.first << " " << v[i].first.second << endl;
			happy += min(v[i].first.first, v[i].first.second);
			if(v[i].first.first > v[i].first.second) {
				auto best = rem.upper_bound(v[i].second);
				if(best != rem.begin()) {
					--best;
					happy += v[i].first.first - v[i].first.second;
					rshift = max(rshift, *best);
					rem.erase(*best);
				}
				else if(v[i].second >= lshift) {
					happy += v[i].first.first - v[i].first.second;
					rem.erase(*best);
					rshift++;	
					lshift++;
				}
				else {
					lshift = min(lshift, *best);
					rem.erase(*best);
				}
			}	
			else {
				auto best = rem.upper_bound(v[i].second);
				if(best != rem.end()) {
					happy += v[i].first.second - v[i].first.first;
					lshift = min(lshift, *best);
					rem.erase(*best);
					continue;
				}
				--best;
				if(v[i].second + 1 <= rshift) {
					happy += v[i].first.second - v[i].first.first;
					rem.erase(*best);
					lshift--;
					rshift--;
				}
				else {
					rshift = max(rshift, *best);
					rem.erase(*best);
				}
			}
		}
		// cout << lshift << " " << rshift << endl;
		cout << happy << endl;
	}
	return 0;
}


















