#include <cstdio>
#include <queue>
#include <algorithm>
#include <ctype.h>
#define DEBUG
const int bufSize = 1e6;
using namespace std;
inline char nc()
{
    #ifdef DEBUG
    return getchar();
    #endif
    static char buf[bufSize],*p1 = buf,*p2 = buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,bufSize,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>
inline T read(T &r)
{
    static char c;
    r=0;
    for(c = nc();!isdigit(c);) c = nc();
    for(;isdigit(c);c=nc()) r = r * 10 + c - 48;
    return r;
}
const int maxn = 2e5 + 100;
int T,n,anum,bnum;
struct node
{
    int L,R,K;
    bool operator<(const node &b)
    {
        return K < b.K;
    }
}A[maxn],B[maxn];
priority_queue<int> q1,q2;
int main()
{
    read(T);
    while(T--)
    {
        anum = bnum = 0;
        long long ans = 0;
        read(n);
        for(int i = 1;i<=n;++i) 
        {
            int k,l,r;
            read(k),read(l),read(r);
            if (l > r) A[++anum].L = l, A[anum].R = r, A[anum].K = k;
            else B[++bnum].L = l, B[bnum].R = r, B[bnum].K = n - k;
            ans += std::min(l,r);
        }
        sort(A + 1,A + 1 + anum),sort(B + 1,B + 1 + bnum);
        for(int i = 1;i<=anum;++i)
        {
            if(q1.size() < A[i].K) q1.push(A[i].L - A[i].R);
            else if(q1.size() == A[i].K && q1.top() < A[i].L - A[i].R) 
                q1.pop(),q1.push(A[i].L - A[i].R);
        }
        for (int i = 1; i <= bnum; ++i)
        {
            if(q2.size() < B[i].K) q2.push(B[i].R - B[i].L);
            else if(!q2.empty() && q2.size() == B[i].K && q2.top() < B[i].R - B[i].L) 
                q2.pop(),q2.push(B[i].R - B[i].L);
        }
        while(!q1.empty()) ans += q1.top(),q1.pop();
        while(!q2.empty()) ans += q2.top(),q2.pop();
        printf("%lld\n",ans);
    }
    return 0;
}