#include <stdio.h>
#include <set>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int N = 2e5+5;
vector<pair<int,int> >lft,rgt;
long long Change(vector<pair<int,int> > a) {
    int n = a.size();
    long long res = 0;
    set<int> pos;
    for (int i = 1;i<= n;i++)
    	pos.insert(i);
    for(auto x:a) {
        auto it = pos.upper_bound(x.second);
        if(it!=pos.begin()) {
            it--;
            res += x.first;
            pos.erase(it);
        }
    }
    return res;
}
void solve() {
	int n;
	scanf("%d",&n);
	lft.clear();
	rgt.clear();
	int l,r,k;
	long long ans = 0;
	for (int i = 1;i<= n;i++) {
		scanf("%d%d%d",&k,&l,&r);
		if (l > r) ans += r,lft.push_back(make_pair(l-r,k));
		else ans += l,rgt.push_back(make_pair(r-l,n-k));
	}
	sort(lft.begin(),lft.end());
	reverse(lft.begin(),lft.end());
	sort(rgt.begin(),rgt.end());
	reverse(rgt.begin(),rgt.end());
	printf("%lld\n",ans+Change(lft)+Change(rgt));
}
int main()
{
	int cas;
	scanf("%d",&cas);
	while (cas--)
		solve();
	return 0;
}

/*
10110111001011011001110
6002382
 */