#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <numeric>
#include <set>

using namespace std;
struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20);};}aaa;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
#define debug(v) {cerr<<"\033[1;36m[debug]\033[m "<<#v<<" : "<<v<<endl;}

using int64 = long long;
using TP = tuple<int64, int64, int64>;

class BIT {
    int n;
    vector<int64> d;
public:
    BIT(int _n=0) {n=_n; d=vector<int64>(n+1);}
    void add(int i, int64 x=1) { for (i++; i <= n; i += i&-i) d[i] += x; }
    int64 sum(int i) { int64 x = 0; for (i++; i; i -= i&-i) x += d[i]; return x; }
};

void solve() {
    int n;
    cin >> n;
    vector<TP> klr;
    for (int i=0; i<n; i++) {
        int64 k,l,r;
        cin >> k >> l >> r;
        klr.emplace_back(k,l,r);
    }

    auto c = [](const auto& x, const auto& y){return max(get<1>(x),get<2>(x)) > max(get<1>(y), get<2>(y)); };
    sort(klr.begin(), klr.end(), c);

    BIT bit(n);
    int64 ans = 0;
    for (int i=0; i<n; i++) {
        const auto& [k,l,r] = klr[i];
        if (k >= n) {
            ans += l;
            bit.add(k);
            continue;
        }

        if (l < r) {
            ans += r;
            bit.add(n);
        } else {
            if (bit.sum(k) < k) {
                ans += l;
                bit.add(k);
            } else {
                ans += r;
                bit.add(n);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    int t;
    cin >> t;
    for (int i=0; i<t; i++) solve();
}