#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lli long long int
#define tc int t;cin>>t;while(t--)
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define pb push_back
#define vi vector<int>
#define vll vector<lli>
#define maxn 505


int main(){
    fastio;
    tc{
        int n;
        cin>>n;
        lli sm=0;
        priority_queue<int> pq[n];
        priority_queue<int,vector<int>,greater<int> > rs;
        for(int i=0;i<n;i++)
        {
            int k,l,r;
            cin>>k>>l>>r;
            sm+=r;
            pq[k-1].push(l-r);
        }
        int comp=0,prv=0;
        for(int i=0;i<n;i++)
        {
            int c1=pq[i].size(),cr=0;
            if(pq[i].empty()) continue;
            while(!pq[i].empty())
            {
                int x=pq[i].top();
                if(rs.size()<i+1)
                {
                    if(rs.size()  and rs.top()<0 and sz>=prv)
                    rs.pop(),rs.push(0),sz--;
                    rs.push(x),sz++;
                }
                else if(x>(int)rs.top())
                rs.pop(),rs.push(x);
                else rs.push(0);
                pq[i].pop();
                cr++;
            }
            comp+=c1;
            prv=i+1;
        }
        while(!rs.empty())
        {
            sm+=(int)rs.top();
            rs.pop();
        }
        cout<<sm<<"\n";
    }
    return 0;
}