#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5;
vector<pair<ll,ll>> a , b;
priority_queue<ll , vector<ll> , greater<ll>>Q;
int main()
{
    ios::sync_with_stdio(false);
    int t; cin >> t;
    while (t--){
        a.clear();
        b.clear();
        int n ; cin >> n;
        ll ans = 0;
        for (int i = 1 ; i <= n ; i++){
            ll k , l , r; cin >> k >> l >> r;
            if (l > r || k == n) ans += r , a.push_back(make_pair(k , l - r));
            else ans += l , b.push_back(make_pair(n - k , r - l));
        }
        sort(a.begin() , a.end());
        sort(b.begin() , b.end());
        for (auto & g : a){
            // 排序之后不可能出现 Q.size() > g.k的情况吧
            if (Q.size() < g.first) Q.push(g.second);
            else if (Q.top() < g.second) Q.pop() , Q.push(g.second);
        }
        while (Q.size()) ans += Q.top() , Q.pop();
        for (auto & g : b){
            if (Q.size() < g.first) Q.push(g.second);
            else if (Q.top() < g.second) Q.pop() , Q.push(g.second);
        }
        while (Q.size()) ans += Q.top() , Q.pop();
        cout << ans << endl;
    }
    return 0;
}