#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

ll calc(vector<pair<int, int>> &a) {
    int n = a.size();
    set<int> free;
    for (int i = 1; i <= n; i++) free.insert(i);
    sort(a.rbegin(), a.rend());
    ll ans = 0;
    for (auto[v, p] : a) {
        auto it = free.upper_bound(p);
        if (it != free.begin()) {
            it--;
            free.erase(it);
            ans += v;
        }
    }
    return ans;
}

void solve() {
    int n;
    scanf("%d", &n);
    vector<pair<int, int>> pos, neg;
    ll tot = 0;
    for (int i = 0; i < n; i++) {
        int k, l, r;
        scanf("%d%d%d", &k, &l, &r);
        if (l > r) {
            tot += r;
            pos.push_back({l - r, k});
        } else {
            tot += l;
            neg.push_back({r - l, n - k});
        }
    }
    printf("%lld\n", tot + calc(pos) + calc(neg));
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) solve();
}
