#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 2e5 + 7;
int k[MAXN], L[MAXN], R[MAXN];
ll solve(vector<pii>&vec) {
    int n = vec.size();
    set<int>s;
    for (int i = 1; i <= n; i++) s.insert(i);
    sort(vec.begin(), vec.end(), greater<pii>());
    ll res = 0;
    for (pii x : vec) {
        auto it = s.upper_bound(x.second);
        if (it == s.begin()) continue;
        it--;
        s.erase(it);
        res += x.first;
    }
    return res;
}
int main() {
    int T; scanf("%d", &T);
    while (T--) {
        int n; scanf("%d", &n);
        long long ans = 0;
        vector<pii>vec1, vec2;
        for (int i = 1; i <= n; i++) {
            scanf("%d%d%d", &k[i], &L[i], &R[i]);
            if (L[i] >= R[i]) vec1.push_back({L[i] - R[i], k[i]}), ans += R[i];
            else vec2.push_back({R[i] - L[i], n - k[i]}), ans += L[i];
        }
        ans += solve(vec1);
        ans += solve(vec2);
        printf("%lld\n", ans);
    }
    return 0;
}