#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

void solve(){
    int n;
    cin >> n;
    ll ans = 0;
    vector<pair<int, int>> A, B;
    for (int i = 0; i < n; ++i) {
        int k, l, r;
        cin >> k >> l >> r;
        ans += max(l, r);
        if(l > r) A.emplace_back(k, l-r);
        else B.emplace_back(n-k, r-l);
    }
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    GPQ<int> s;
    for (auto &&e : A) {
        s.emplace(e.second);
        while(s.size() > e.first){
            ans -= s.top();
            s.pop();
        }
    }
    while(!s.empty()) s.pop();
    for (auto &&e : B) {
        s.emplace(e.second);
        while(s.size() > e.first){
            ans -= s.top();
            s.pop();
        }
    }
    cout << ans << "\n";
}

int main() {
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}