#include<bits/stdc++.h>

using namespace std;
#define int long long
#define endl '\n'

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t; cin >> t;
    for (; t; t--) {
        int n; cin >> n;
        vector<int> k(n), l(n), r(n);
        vector<pair<int, int>> vl(n);
        for (int i = 0; i < n; i++) {
            cin >> k[i] >> l[i] >> r[i];
            k[i]--;
            vl[i] = {(l[i] - r[i]), i};
        }
        sort(vl.rbegin(), vl.rend());
        int ans = 0;
        set<int> q;
        for (int i = 0; i < n; i++) q.insert(i);
        for (int i = 0; i < n; i++) {
            if (vl[i].first <= 0) break;
            int id = vl[i].second;
            auto t = q.upper_bound(k[id]);
            if (t == q.begin()) {
                ans += r[id];
            }
            else {
                ans += l[id];
                t--;
                q.erase(t);
            }
        }
        for (int i = 0; i < n; i++) q.insert(i);
        for (int i = n - 1; i >= 0; i--) {
            if (vl[i].first > 0) break;
            int id = vl[i].second;
            auto t = q.upper_bound(k[id]);
            if (t == q.end()) ans += l[id];
            else {
                ans += r[id];
                q.erase(t);
            }
        }
        cout << ans << endl;
    }
    return 0;
}
