#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

struct UnionFind{
private:
    vector<int> par;
    vector<int> siz;
public:
    void init(int N){
        par.resize(N);
        siz.resize(N);
        for(int i=0; i<N; i++) par[i] = i;
        for(int i=0; i<N; i++) siz[i] = 1;
    }
    void unite(int a, int b){
        int rootA = root(a);
        int rootB = root(b);
        if(rootA == rootB) return;
        par[rootB] = rootA;
        siz[rootA] += siz[rootB];
    }
    int root(int a){
        if(par[a] == a) return a;
        return par[a] = root(par[a]);
    }
    bool same(int a, int b){
        return root(a) == root(b);
    }
    int size(int a){
        return siz[root(a)];
    }
};

int T;

signed main(){
    cin >> T;
    rep(t, T){
        int N;
        cin >> N;
        vector<pair<ll, int>> v(N);
        ll ans = 0;
        rep(i, N){
            int k;
            ll L, R;
            cin >> k >> L >> R;
            ans += R;
            v[i] = mp(L-R, k);
        }
        sort(all(v));
        reverse(all(v));
        
        vector<bool> used(N, false);
        UnionFind uf;
        uf.init(N);
        int i;
        for(i=0; i<N && v[i].first >= 0; i++){
            int idx = uf.root(v[i].second-1);
            if(used[idx] == false){
                ans += v[i].first;
                used[idx] = true;
                if(idx > 0) uf.unite(idx-1, idx);
            }
        }
        for(; i<N; i++){
            if(i < v[i].second) ans += v[i].first;
        }
        cout << ans << endl;
    }
}