#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
using namespace std;
typedef long long int ll;
using ull = unsigned long long;

ll solve(vector<pair<int,int>> v){
    int n=v.size();
    for(int i=0;i<n;i++){
        v[i].first=min(v[i].first,n);
    }
    vector<vector<int>> s(n+1);
    for(int i=0;i<n;i++){
        s[v[i].first].push_back(v[i].second);
    }
    ll res=0;
    priority_queue<ll> q;
    for(int i=n;i>=1;i--){
        for(int x:s[i]){q.push(x);}
        if(q.size()){
            res+=q.top();
            q.pop();
        }
    }
    return res;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        int n; cin >> n;
        vector<pair<int,int>> pl,pr;
        ll res=0;
        for(int i=0;i<n;i++){
            int k,l,r; cin >> k >> l >> r;
            int m=min(l,r);
            res+=m;
            l-=m,r-=m;
            if(l>0)pl.push_back({k,l});
            else pr.push_back({n-k,r});
        }
        res+=solve(pl);
        res+=solve(pr);
        printf("%lld\n",res );
    }
}