#include <bits/stdc++.h>
#include <stdlib.h>
#include <time.h> 
 
using namespace std;
long long mod = 1e9 + 7;
long long INF = 1e7;
double EPS = 1e-12;
typedef long long int lld;
long double tick(){static clock_t oldt; clock_t newt=clock();
    long double diff=1.0L*(newt-oldt)/CLOCKS_PER_SEC; oldt = newt; return diff; }
#define rep(i,a,n) for(long long int i = (a); i <= (n); ++i)
#define repI(i,a,n) for(int i = (a); i <= (n); ++i)
#define repD(i,a,n) for(long long int i = (a); i >= (n); --i)
#define repDI(i,a,n) for(int i = (a); i >= (n); --i)
inline lld sc() { lld a; scanf("%lld", &a); return a; }
inline int scd() { int a; scanf("%d", &a); return a; }
inline double scf() { double a; scanf("%lf",&a); return a; }
#define prL(a) printf("%lld\n",a)
#define prS(a) printf("%lld ",a)
#define prdL(a) printf("%d\n",a)
#define prdS(a) printf("%d ",a)
#define all(c) (c).begin(), (c).end()
#define sz(a) ((int)a.size())
#define Error(x...) { cout << "(" << #x << ")" << " = ( "; printIt(x); }
template <typename T1> void printIt(T1 t1) { cout << t1 << " )" << endl; }
template <typename T1, typename... T2>
void printIt(T1 t1, T2... t2) { cout << t1 << " , "; printIt(t2...); }
#define errorpair(a) cout<<#a<<" = ( "<<((a).first)<<" , "<<((a).second)<<" )\n";
#define mset(a, v) memset(a, v, sizeof(a))
#define popcount __builtin_popcountll

typedef pair<lld,lld> PA;

lld powP(lld x, lld y) {
    lld ans = 1, po = x % mod;
    while(y > 0) {
        if(y & 1) ans = ans * po % mod;
        y >>= 1; po = po * po % mod;
    }
    return ans;
}
 
bool bitSet(lld n, lld bit){
    if((n & (1LL << bit)) != 0) return true;
    else return false;
}
 
lld findGcd(lld a, lld b){
    while(a != 0) { lld temp = b % a; b = a; a = temp; }
    return b;
}

#define lim 300010
#define lim2 200010
#define qqqqqq 210
// Ab :)
// inline lld sqr(lld x) { return x * x; }
// unordered_map<lld,lld>::iterator it;
// std::ios::sync_with_stdio(false);
// priority_queue<PA> Q;
// lld dp[1<<18];

// vector<lld> G[lim];
// string S[lim];
lld A[lim];

lld Fen[lim];

lld findSumF(lld Fen[],lld ind,lld n){
    lld i,ans=0;
    for(i=ind;i>0;i-=(i&-i)){
        ans+=Fen[i];
    }
    return ans;
}

lld findSum(lld Fen[],lld l,lld r,lld n){
    return findSumF(Fen,r,n)-findSumF(Fen,l-1,n);
}

void update(lld Fen[],lld ind,lld val,lld n){
    // Error(ind, n);
    lld i;
    for(i=ind;i<=n;i+=(i&-i)){
        Fen[i]+=val;
    }
}

lld findLastZero(lld high, lld n) {
    lld low = 1, ans = -1;
    while(low <= high) {
        lld mid = low + (high - low) / 2;
        lld slots = high - mid + 1;
        lld sum = findSum(Fen, mid, high, n);
        if(sum < slots) {
            ans = mid;
            low = mid + 1;
        }
        else high = mid - 1;
    }
    return ans;
}

lld findFirstZero(lld low, lld n) {
    lld high = n, ans = -1;
    while(low <= high) {
        lld mid = low + (high - low) / 2;
        lld slots = mid - low + 1;
        lld sum = findSum(Fen, low, mid, n);
        if(sum < slots) {
            ans = mid;
            high = mid - 1;
        }
        else low = mid + 1;
    }
    return ans;
}


multiset<PA> PosCamels, NegCamels;
int main(){
    int t = scd();
    while(t--) {
        lld n = sc();
        PosCamels.clear();
        NegCamels.clear();
        lld ans = 0;
        lld pos = 0, neg = 0;
        rep(i,1,n) {
            lld k = sc(), l = sc(), r = sc();
            ans += r;
            if(l >= r) PosCamels.insert({r - l, k}), pos++;
            else NegCamels.insert({l - r, k}), neg++;
        }
        rep(i,0,n) Fen[i] = 0;
        for(auto& pio : PosCamels) {
            lld k = pio.second, loss = pio.first;
            lld ind = findLastZero(min(k, pos), n);
            // Error(loss, k, ind, ans);
            if(ind == -1) ind = findLastZero(pos, n);
            else ans -= loss;
            update(Fen, ind, 1, n);
        }
        for(auto& pio : NegCamels) {
            lld k = pio.second, loss = pio.first;
            lld ind = findFirstZero(k + 1, n);
            // Error(loss, k + 1, ind, ans);
            if(ind == -1) ind = findFirstZero(pos + 1, n), ans += loss;
            // else ans += loss;
            // Error(loss, pos + 1, ind, ans);
            // Error(pos + 1, n, ind);
            update(Fen, ind, 1, n);
        }
        prL(ans);
    }
    return 0;
}


Measured memory_bytes: 17816