#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cassert>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

void solve() {
    int n;
    cin >> n;
    vector<ll> k(n), l(n), r(n);
    vector<vector<int>> S(n);
    REP(i, n) {
        cin >> k[i] >> l[i] >> r[i];
        --k[i];
        S[k[i]].push_back(i);
    }
    
    ll ans = 0;
    REP(i, n) ans += min(l[i], r[i]);
    priority_queue<ll> pq;
    for (int i = n - 1; i >= 0; --i) {
        for (int j: S[i]) {
            if (l[j] > r[j]) pq.push(l[j] - r[j]);
        }
        if (!pq.empty()) {
            ans += pq.top();
            pq.pop();
        }
    }
    while (!pq.empty()) pq.pop();
    for (int i = 1; i < n; ++i) {
        for (int j: S[i - 1]) {
            if (l[j] <= r[j]) pq.push(r[j] - l[j]);
        }
        if (!pq.empty()) {
            ans += pq.top();
            pq.pop();
        }
    }
    
    cout << ans << "\n";
    
}

int main() {
    int t;
    cin >> t;
    REP(i, t) solve();
}
