#include <cstdio>
#include <algorithm>
#include <set>
#include <ctype.h>
using namespace std;
const int bufSize = 1e6;
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;
struct node
{
    int id,L,R,K;
    bool operator<(const node &b)
    {
        return R - L < b.R - b.L;
    }
}A[maxn],B[maxn];
bool cmplr(const node &a,const node &b)
{
    if(a.K == b.K) return a.L - a.R > b.L - b.R;
    return a.K < b.K;
}
int T,n;
int anum,bnum;
//priority_queue<node,vector<node>,greater<node> > s;
int main()
{
    read(T);
    while(T--)
    {
        anum = bnum = 0;
        read(n);
        for(int i = 1;i<=n;++i)
        {
            int a,b,c;
            read(a),read(b),read(c);
            if(b > c) A[++anum].K = a,A[anum].L = b,A[anum].R = c;
            else B[++bnum].K = a,B[bnum].L = b,B[bnum].R = c;
        }
        sort(A + 1,A + 1 + anum,cmplr);
        sort(B + 1,B + 1 + bnum);
        long long sum = 0;
        for(int i = 1;i<=anum;++i)
        {
            if(i <= A[i].K) sum += A[i].L;
            else sum += A[i].R;
         //   printf("%d %d %d %d %d\n",i,A[i].K,A[i].L,A[i].R,sum);
        }
        for(int i = 1;i<=bnum;++i) 
        {
            if(anum + i <= B[i].K) sum += B[i].L;
            else sum += B[i].R;            
           // printf("%d %d %d %d %d\n",anum + i,B[i].K,B[i].L,B[i].R,sum);
        }
        printf("%lld\n",sum);
    }
    return 0;
}