 // In the name of God //
 
#include <bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pii pair <int, int>
using namespace std;
const ll mod = 998244353;

ll calc(vector<pii> &a) {
    int n = a.size();
    set<int> free;
    ll ans = 0;
    sort(rall(a));
    for (int i = 1; i <= n; i++)    free.insert(i);
    for (auto p : a) {
        auto it = free.upper_bound(p.second);
        if(it != free.begin()) {
            ans += p.first;
            --it;
            free.erase(it);
        }
    }
    return ans;
}

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int>> pos, neg;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int k, l ,r;
        cin >> k >> l >> r;
        if(l > r) {
            ans += r;
            pos.push_back({l - r, k});
        }
        else {
            ans += l;
            neg.push_back({r - l, n - k});
        }
    }
    cout << ans + calc(pos) + calc(neg) << "\n";
}

int main()  {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    int tt;
    cin >> tt;
    for (int tc = 1; tc <= tt; ++tc) {
        solve();
    } 
}                               