#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <random>
#include <set>
#include <unordered_set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <ctime>
#include <limits.h>
#include <chrono>

#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);

typedef long long ll;
typedef long double ld;

#define pb push_back
#define ins insert
#define er erase

#define f(i, a, b) for (int i = a; i < b; ++i)
#define rf(i, a, b) for (int i = a; i >= b; --i)
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define cadd(a, b) a = add(a, b)
#define csub(a, b) a = subt(a, b)
#define cmul(a, b) a = mult(a, b)
#define mem(a, b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()
#define lb lower_bound
#define ub upper_bound
#define mxe max_element
#define mne min_element

#define srt(v) sort(all(v))
#define rsrt(v) sort(v.rbegin(), v.rend())

#define ff first
#define ss second
#define sz(a) (int)((a).size())

#define bpc(a) __builtin_popcount(a)
#define bpcl(a) __builtin_popcountll(a)
#define prec(a) fixed << setprecision(a)

#define dbg(a, st, end)                   \
    f(i, st, end + 1) cerr << a[i] << sp; \
    cerr << nl;
#define pnt(a, st, end)                   \
    f(i, st, end + 1) cout << a[i] << sp; \
    cout << nl;
#define dbgc(a)          \
    for (auto y : a)     \
        cerr << y << sp; \
    cerr << nl;
#define dbgm(a)                            \
    for (auto y : a)                       \
        cerr << y.ff << "," << y.ss << sp; \
    cerr << nl;

#define trace(a) cerr << a << nl;
#define trace2(a, b) cerr << a << sp << b << nl;
#define trace3(a, b, c) cerr << a << sp << b << sp << c << nl;
#define trace4(a, b, c, d) cerr << a << sp << b << sp << c << sp << d << nl;

#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a)*1ll * (b)) / gcd(a, b)

#define nl "\n"
#define sp " "

using namespace std;
using namespace std::chrono;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vst;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;
typedef set<int> si;
typedef unordered_set<int> usi;
typedef set<pii> spii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

ll md = 1000000009;
ld pi = 3.141592653;
//ll md = 998244353;

ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m);
        m = (m * m);
        b /= 2;
    }
    return c;
}

ll pwmd(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pwmd(n, md - 2);
}

ll my_ceil(ll a, ll b)
{
    if (a % b == 0)
        return a / b;
    return a / b + 1;
}

ll nc2(ll n)
{
    return (1ll * n * (n - 1)) / 2;
}

ll nsum(ll n)
{
    return (1ll * n * (n + 1)) / 2;
}

int my_log(ll n, int b)
{
    ll i = 1;
    int ans = 0;
    while (1)
    {
        if (i > n / b)
            break;
        i *= b;
        ans++;
    }
    return ans;
}

inline int nc()
{
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline int read()
{
    int ret = 0;
    bool f = 0;
    char ch = nc();
    while (ch > '9' || ch < '0')
        f ^= ch == '-', ch = nc();
    while (ch <= '9' && ch >= '0')
        ret = ret * 10 + ch - '0', ch = nc();
    return f ? -ret : ret;
}

inline int randint(int l, int r)
{
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    return uniform_int_distribution<int>(l, r)(rng);
}

inline ll randll(ll l, ll r)
{
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
    return uniform_int_distribution<ll>(l, r)(rng);
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

inline ll add(ll a, ll b) { return (((a >= md) ? a % md : a) + ((b >= md) ? b % md : b) + md) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * ((a >= md) ? a % md : a) * ((b >= md) ? b % md : b)) % md; }

inline int manhatten(pii p1, pii p2) { return abs(p1.ff - p2.ff) + abs(p1.ss - p2.ss); }

inline bool kthbit(ll n, int k) { return (n >> k) & 1; }
inline ll setkthbit(ll n, int k) { return n | (1ll << k); }
inline ll unsetkthbit(ll n, int k) { return n & ~(1ll << k); }
inline ll flipkthbit(ll n, int k) { return n ^ (1ll << k); }

#define int long long //UNCOMMENT IF NEEDED

/* 
    You have to stop the Q-Tip when there's resistance!
                                - Chandler Bing, S02E01
*/

int32_t main()
{
    fast;

    int h, e, k, n, m, y;
    int x, q, l, r, c;

    string s, t;

    auto start = high_resolution_clock::now();

    int tcnt = 0;

    int tests = 1;
    //pre();
    cin >> tests;

    while (tests--)
    {
        //cout << "Case #" << ++tcnt << ": ";

        cin >> n;
        int L[n + 1], R[n + 1], K[n + 1];
        ll ans = 0;
        vpii fr, bc;
        f(i, 1, 1 + n)
        {
            cin >> K[i] >> L[i] >> R[i];
            ans += min(L[i], R[i]);
            if (L[i] >= R[i])
                fr.pb({K[i], i});
            else
                bc.pb({K[i], i});
        }
        srt(fr);
        rsrt(bc);
        int acc = 0;
        spii taken;
        for (auto y : fr)
        {
            k = y.ff;
            e = y.ss;
            if (acc == k)
            {
                if (!taken.empty() && taken.begin()->ff < L[e] - R[e])
                {
                    ans -= taken.begin()->ff;
                    taken.er(taken.begin());
                    acc--;
                }
                else
                    continue;
            }
            acc++;
            taken.ins({L[e] - R[e], e});
            ans += L[e] - R[e];
        } //dbgm(taken);trace(ans);
        taken.clear();
        acc = 0;
        for (auto y : bc)
        {
            k = y.ff;
            e = y.ss;
            if (acc == n - k)
            {
                if (!taken.empty() && taken.begin()->ff < R[e] - L[e])
                {
                    ans -= taken.begin()->ff;
                    taken.er(taken.begin());
                    acc--;
                }
                else
                    continue;
            }
            acc++;
            taken.ins({R[e] - L[e], e});
            ans += R[e] - L[e];
        }
        cout << ans << nl;

        // RESTORE SEGTREE SIZE
    }
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cerr << "Time taken: "
         << duration.count() / 1000000.0 << "seconds" << nl;
    return 0;
}


Measured memory_bytes: 14328