#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve() {
    int t;
    cin >> t;
    REP(ttt,0,t) {
        int n;
        cin >> n;
        set<int> s;
        REP(i,0,n) s.insert(i);

        priority_queue<pair<int,PII>, vector<pair<int,PII>>> ql, qr;
        int k,l,r;
        // vector<bool> used(n);
        VP ps(n);
        REP(i,0,n) {
            cin >> k >> l >> r;
            if (l-r>=0) ql.emplace(l - r, MP(k-1,i));
            else qr.emplace(r - l, MP(k-1,i));
            ps[i] = MP(l,r);
        }
        LL sum = 0;

        {
            set<int> s;
            REP(i,0,ql.size()) s.insert(i);
            while (!ql.empty()) {
                auto p = ql.top();
                ql.pop();

                int k = p.second.first, idx = p.second.second;
                auto it = s.upper_bound(k);
                // dump(it == s.begin());
                // dump(idx);
                if (it == s.begin()) {
                    sum += ps[idx].second;
                } else {
                    --it;
                    sum += ps[idx].first;
                    s.erase(it);
                }
                // dump(sum);
            }
        }
        {
            set<int> s;
            REP(i,n-qr.size(),n) s.insert(-i);
            while (!qr.empty()) {
                auto p = qr.top();
                qr.pop();

                int k = p.second.first, idx = p.second.second;
                auto it = s.upper_bound(-k-1);
                // dump(it == s.begin());
                // dump(idx);
                if (it == s.begin()) {
                    sum += ps[idx].first;
                } else {
                    --it;
                    sum += ps[idx].second;
                    s.erase(it);
                }
                // dump(sum);
            }
        }

        cout << sum << endl;
    }
}

signed main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 21320