// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void solve() {
    int N; scanf("%d", &N);
    ll ans = 0;

    vector< tuple<ll, ll, ll> > posi, nega;
    for(int i=0; i<N; i++) {
        ll k, l, r; scanf("%lld%lld%lld", &k, &l, &r);
        if(l == r) {
            ans += l;
        }
        else if(k == N) {
            ans += l;
        }
        else if(l < r) {
            nega.emplace_back(k, l, r);
        }
        else if(l > r) {
            posi.emplace_back(k, l, r);
        }
    }

    vector< vector< pair<ll, ll> > > posi_c(N+1), nega_c(N+1);
    for(auto e : posi) {
        ll k, l, r; tie(k, l, r) = e;
        posi_c[k].emplace_back(l, r);
    }
    for(auto e : nega) {
        ll k, l, r; tie(k, l, r) = e;
        nega_c[k].emplace_back(l, r);
    }

    int cnt = 0;
    {
        priority_queue< tuple<ll, ll, ll> > que;
        for(int i=N; i>=1; i--) {
            for(auto e : posi_c[i]) {
                ll l, r; tie(l, r) = e;
                que.emplace(l-r, l, r);
            }
            if(que.size()) {
                cnt++;
                ll v, l, r; tie(v, l, r) = que.top(); que.pop();
                ans += l;
            }
        }
        while(que.size()) {
            ll v, l, r; tie(v, l, r) = que.top(); que.pop();
            ans += r;
        }
    }
    {
        priority_queue< tuple<ll, ll, ll> > que;
        for(int i=1; i<=N; i++) {
            for(auto e : nega_c[i-1]) {
                ll l, r; tie(l, r) = e;
                que.emplace(r-l, l, r);
            }
            if(i > cnt and que.size()) {
                ll v, l, r; tie(v, l, r) = que.top(); que.pop();
                ans += r;
            }
        }
        while(que.size()) {
            ll v, l, r; tie(v, l, r) = que.top(); que.pop();
            ans += l;
        }
    }

    /*
    sort(posi.begin(), posi.end(), [](auto x, auto y) {
            ll k1, l1, r1; tie(k1, l1, r1) = x;
            ll k2, l2, r2; tie(k2, l2, r2) = y;
            if(k1 != k2) return k1 < k2;
            return l1-r1 > l2-r2;
        });
    sort(nega.begin(), nega.end(), [](auto x, auto y) {
            ll k1, l1, r1; tie(k1, l1, r1) = x;
            ll k2, l2, r2; tie(k2, l2, r2) = y;
            if(k1 != k2) return k1 > k2;
            return l1-r1 < l2-r2;
        });

    {
        int ptr = 0;
        for(int i=0; i<(int)posi.size(); i++) {
            ll k, l, r; tie(k, l, r) = posi[i];
            // fprintf(stderr, "posi: %lld %lld %lld\n", k, l, r);
            // おける
            if(ptr < k) {
                // fprintf(stderr, "can !\n");
                ans += l;
                ptr++;
            }
            // おけない (余ったところに適当に置く)
            else {
                // fprintf(stderr, "cannot !\n");
                ans += r;
            }
        }
    }
    {
        int ptr = N;
        for(int i=0; i<(int)nega.size(); i++) {
            ll k, l, r; tie(k, l, r) = nega[i];
            // fprintf(stderr, "nega: %lld %lld %lld\n", k, l, r);
            // おける
            if(ptr > k) {
                // fprintf(stderr, "can !\n");
                ans += r;
                ptr--;
            }
            else {
                // fprintf(stderr, "cannot !\n");                
                ans += l;
            }
        }
    }
    */
    printf("%lld\n", ans);
}

int main() {
    int T; scanf("%d", &T);
    while(T--) solve();
    return 0;
}


Measured memory_bytes: 27240