#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>
#include <queue>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

void solve() {
    int N;
    cin >> N;

    vector<int> diff1[N + 1], diff2[N + 2];
    ll ans = 0;
    REP(i, N) {
        int K, L, R;
        cin >> K >> L >> R;
        if (L >= R) diff1[K].push_back(L - R);
        else diff2[K + 1].push_back(R - L);
        ans += min(L, R);
    }

    priority_queue<int, vector<int>, greater<int>> que;
    FOR(i, 1, N + 1) {
        for (int d : diff1[i]) que.push(d);
        while (que.size() > i) que.pop();
    }
    while (!que.empty()) {
        ans += que.top(); que.pop();
    }

    FOR(i, 1, N + 1) {
        for (int d : diff2[N + 1 - i]) que.push(d);
        while (que.size() > i) que.pop();
    }
    while (!que.empty()) {
        ans += que.top(); que.pop();
    }

    cout << ans << endl;
}

int main() {
    int T;
    cin >> T;
    REP(i, T) solve();
    return 0;
}