#include<bits/stdc++.h>

int main(){
    using namespace std;
    unsigned long T;
    cin >> T;
    for(unsigned long iter{0}, N; iter < T; ++iter){
        cin >> N;
        vector<pair<unsigned long, unsigned long>> f, b;
        f.reserve(N);
        unsigned long ans{0};
        for(unsigned long i{0}, k, l, r; i < N; ++i){
            cin >> k >> l >> r;
            if(l > r){
                ans += r;
                f.emplace_back(k - 1, l - r);
            }else{
                ans += l;
                if(k < N)b.emplace_back(N - k - 1, r - l);
            }
        }
        sort(begin(f), end(f));
        sort(begin(b), end(b));
        vector<unsigned long> tmp(N);
        unsigned long now{0};
        for(const auto& [k, c] : f){
            if(tmp[now])++now;
            now = min({now, k, size(f)});
            tmp[now] = max(tmp[now], c);
        }
        now = 0;
        reverse(begin(tmp), end(tmp));
        for(const auto& [k, c] : b){
            if(tmp[now])++now;
            now = min({now, k, size(b)});
            tmp[now] = max(tmp[now], c);
        }
        cout << accumulate(begin(tmp), end(tmp), ans) << endl;
    }
    return 0;
}