#include <bits/stdc++.h>

using namespace std;

// This is the template of Errichto which can be found on https://codeforces.com/submissions/Errichto

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {

sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
#define LOCAL 1
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

//int dirs[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
//int dirs[8][2]={{-1,0},{0,-1},{0,1},{1,0},{1,1},{-1,-1},{-1,1},{1,-1}};

vector<int> ft0;
vector<int> ft1;

int n;

void increment(int idx, int which) // zero based idx
{
    idx++;
    for(int i=idx; i<=n; i+=(i&(-i)))
    {
        if(which==0) ft0[i]++;
        else ft1[i]++;
    }
}

long long get_sum(int from, int which)
{
    int ret=0;
    for(int i=from; i>0; i-=(i&(-i)))
    {
        if(which==0) ret+=ft0[i];
        else ret+=ft1[i];
    }
    return ret;
}

long long get_sum(int from, int to, int which) // zero_based indices
{
    if(from>to) return 0;
    return get_sum(to+1,which)-get_sum(from,which);
}

/*
long long solve(vector< pair< pair<long long, long long>, int> > camelsLBigger, int which)
{
    sort(camelsLBigger.begin(),camelsLBigger.end());
    reverse(camelsLBigger.begin(),camelsLBigger.end());
    long long ans=0;
    for(int i=0; i<camelsLBigger.size(); i++)
    {
        long long L=camelsLBigger[i].first.first;
        long long R=camelsLBigger[i].first.second;
        long long sum=get_sum(0,camelsLBigger[i].second-1,which);
        //debug() << imie(sum) << imie(camelsLBigger[i].second) << imie(L) << imie(R);
        if(sum<camelsLBigger[i].second)
        {
                //debug() << imie(i);
            ans+=L;
            ans+=R;
            increment(camelsLBigger[i].second-1,which);
        }
        else
        {
            ans+=R;
        }
    }
    return ans;
}
*/

long long solve(vector< pair< pair<long long, long long>, int> > camelsLBigger, int which)
{
    long long ans=0;
    vector< pair<int, long long> > sortedByDate;
    for(int i=0; i<camelsLBigger.size(); i++)
    {
        ans+=camelsLBigger[i].first.second;
        sortedByDate.push_back(make_pair(camelsLBigger[i].second, camelsLBigger[i].first.first));
    }
    sort(sortedByDate.begin(),sortedByDate.end());
    priority_queue<long long, vector<long long>, greater<long long> > pq;
    for(int i=0; i<sortedByDate.size(); i++)
    {
        int amount=sortedByDate[i].first;
        long long val=sortedByDate[i].second;
        while(pq.size()>=amount && pq.size()>0 && pq.top()<val) pq.pop();
        if(pq.size()<amount) pq.push(val);
    }
    while(!pq.empty())
    {
        long long val=pq.top();
        pq.pop();
        ans+=val;
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    //freopen("inp.in","r",stdin);
    //freopen("outp.out","w",stdout);
    int tc;
    cin >> tc;
    for(int case_no=1; case_no<=tc; case_no++)
    {
        cin >> n;
        ft0.assign(n+1,0);
        ft1.assign(n+1,0);
        vector< pair< pair<long long, long long>, int> > camelsLBigger; // <<L,R>,K>
        vector< pair< pair<long long, long long>, int> > camelsRBigger; // <<R,L>,K>
        long long ans=0;
        for(int i=0; i<n; i++)
        {
            int K;
            long long L, R;
            cin >> K >> L >> R;
            if(L>R)
            {
                camelsLBigger.push_back(make_pair(make_pair(L-R,R),K));
            }
            else if(R>L)
            {
                camelsRBigger.push_back(make_pair(make_pair(R-L,L),n-K));
            }
            else
            {
                ans+=L;
            }
        }
        //debug() << imie(camelsLBigger.size()) << imie(camelsRBigger.size());
        ans+=solve(camelsLBigger,0);
        ans+=solve(camelsRBigger,1);
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 15316