


#include <bits/stdc++.h>

using namespace std;

long long solve(const vector<pair<int, int> > &F) {
    long long ret = 0;
    priority_queue<int, vector<int>, greater<int> > pq;
    for (auto p : F) {
        pq.push(p.second);
        ret += p.second;
        if (pq.size() > p.first) {
            ret -= pq.top();
            pq.pop();
        }
    }
    return ret;
}

void run_case() {
    int n;
    cin >> n;
    vector<pair<int, int> > F, S;
    long long ans = 0;
    int k, l, r;
    for (int i = 0; i < n; i++) {
        cin >> k >> l >> r;
        ans += min(l, r);
        if (l >= r) {
            if (l == r)continue;
            F.push_back({k, l - r});
        } else {
            if (k == n)continue;
            S.push_back({n - k, r - l});
        }
    }
    sort(F.begin(), F.end());
    sort(S.begin(), S.end());
    cout << ans + solve(F) + solve(S) << "\n";
}


int main() {


    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)run_case();


    return 0;
}


