#include <bits/stdc++.h>

using namespace std;

const int max_n = 200222, inf = 1000111222;

int t, n;

long long solve(vector<pair<int, int>> &v) {
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    long long res = 0;
    set<int> q;
    for (int i = 0; i < n; ++i) {
        q.insert(i);
    }
    for (auto p : v) {
        auto it = q.lower_bound(p.second);
        if (it == q.begin()) {
            continue;
        }
        --it;
        q.erase(it);
        res += p.first;
    }
    return res;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        long long ans = 0;
        vector<pair<int, int>> a, b;
        for (int i = 0; i < n; ++i) {
            int k, l, r;
            scanf("%d%d%d", &k, &l, &r);
            ans += min(l, r);
            if (l > r) {
                a.push_back({l - r, k});
            } else {
                b.push_back({r - l, n - k});
            }
        }
        ans += solve(a);
        ans += solve(b);
        printf("%lld\n", ans);
    }
    return 0;
}
