#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
struct ele{
	int k, l, r;
	bool operator<(const ele& other) const
	{
		return abs(l - r) . abs(other.l - other.r);
	}
};
bool cmp(const ele& x, const ele& y)
{
	return x.k < y.k;
}
int main()
{
    cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false);
    int t, n;
    cin >> t;
    while(t--)
    {
    	cin >> n;
    	vector<ele> big, small;
    	ll ans = 0;
    	for(int i = 0; i < n; i++)
    	{
    		ele tmp;
    		cin >> tmp.k >> tmp.l >> tmp.r;
    		if(tmp.k < n && tmp.l != tmp.r)
    		{
    			if(tmp.l > tmp.r)
					big.push_back(tmp);
				else
					small.push_back(tmp);
    		}
    		else
    			ans += tmp.l;
    	}
    	sort(big.begin(), big.end(), cmp);
    	priority_queue<ele> q;
    	for(ele &x: big)
    	{
			q.push(x);
			if(x.k < q.size())
			{
				ans += q.top().r;
				q.pop();
			}
    	}
    	while(!q.empty())
		{
    		ans += q.top().l;
    		q.pop();
		}
    	sort(small.rbegin(), small.rend(), cmp);
    	for(ele &x: small)
		{
			q.push(x);
			if(n - x.k < q.size())
			{
				ans += q.top().l;
				q.pop();
			}
		}
		while(!q.empty())
		{
			ans += q.top().r;
			q.pop();
		}
    	cout << ans << endl;
    }
}