#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <tuple>
#include <algorithm>
#include <queue>
#define rep(i,n) for (int i=0; i<n; ++i)
using namespace std;

struct camel {
    int k=0,h=0;
};
using camelgroup = vector<camel>;

void extract(priority_queue<int>& pool, camelgroup& t, int k) {
    while (t.size()) {
        if (t.back().k >= k) {
            pool.push(t.back().h);
            t.pop_back();
            continue;
        }
        break;
    }
}

int main() {
    int t;
    cin >> t;
    rep(i,t) {
        camelgroup red, blue;
        int ans = 0;
        int n;
        cin >> n;
        rep(j,n) {
            int k,l,r;
            cin >> k >> l >> r;
            int m = min(l, r);
            l -= m;
            r -= m;
            ans += m;
            if (l > r)
                red.push_back({k,l});
            else
                blue.push_back({n-k,r});
        }

        auto lessk = [](camel& c1, camel& c2){ return c1.k < c2.k; };
        sort(red.begin(), red.end(), lessk);
        sort(blue.begin(), blue.end(), lessk);

        auto dis = [&](camelgroup& g){
            priority_queue<int> pool;
            for (int k=g.size(); k > 0; --k) {
                extract(pool, g, k);
                if (pool.size()) {
                    ans += pool.top();
                    pool.pop();
                }
            }
        };
        dis(red);
        dis(blue);

        cout << ans << endl;
    }
}
