#include <bits/stdc++.h>
using namespace std;

typedef tuple<int, int, int> T;
T a[222222];
int main() {
    int t;
    cin >> t;
    for(int i = 0; i < t; i++) {
        int n;
        cin >> n;
        for(int j = 0; j < n; j++) {
            int k, l, r;
            cin >> k >> l >> r;
            a[j] = {r - l, l, k};
        }
        sort(a, a + n, [](auto x, auto y) { return get<2>(x) < get<2>(y); });
        priority_queue<T> q, Q;
        long s = 0;
        for(int j = 0; j < n; j++) {
            auto [m, l, k] = a[j];
            if(m > 0)
                continue;
            q.push(a[j]), s += l;
            if(q.size() > k)
                s += get<0>(q.top()), q.pop();
        }
        for(int j = n; j--;) {
            auto [m, l, k] = a[j];
            if(m <= 0)
                continue;
            Q.push(a[j]), s += m + l;
            if(Q.size() > n - k)
                s -= get<0>(Q.top()), Q.pop();
        }
        cout << s << '\n';
    }
    return 0;
}
