//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    int t; cin >> t;
    for(int _ = 0; _ < t; _++){
        int n; cin >> n;
        // int nl = 0, nr = 0;
        vector<int> ks(n), ls(n), rs(n);
        for(int i = 0; i < n; i++){
            cin >> ks[i] >> ls[i] >> rs[i];
            ks[i]--;
            // (ls[i] < rs[i] ? nr : nl)++;
        }
        // vector<int> lls(nl), rrs(nr);
        // iota(lls.begin(), lls.end(), 0);
        // iota(rrs.begin(), rrs.end(), 0);
        vector<int> lls(n), rrs(n);
        for(int i = 0; i < n; i++){
            lls[i] = i;
            rrs[i] = i;
        }
        vector<int> is(n);
        iota(is.begin(), is.end(), 0);
        sort(is.begin(), is.end(), [&](int i, int j){ return abs(ls[i] - rs[i]) > abs(ls[j] - rs[j]); });
        ll ans = 0;
        for(auto& i : is){
            if(ls[i] < rs[i]){
                int pos = ks[i] + 1;
                if(pos == n){
                    ans += ls[i];
                }else{
                    int idx = rrs[pos];
                    if(idx < n){
                        // rrs[idx] = rrs[idx] + 1 < n ? rrs[rrs[idx] + 1] : n;
                        bool ok = false;
                        while(!ok && idx < n){
                            ok = idx == rrs[idx];
                            rrs[idx] = idx + 1 < n ? rrs[idx + 1] : n;
                            idx = rrs[idx];
                        }
                        ans += ok ? rs[i] : ls[i];
                        // if(idx < n && rrs[idx] != idx){
                        //     rrs[idx] = n;
                        // }
                        // // rrs[idx] = idx + 1 < n ? rrs[idx + 1] : n;
                        // // rrs[pos] = idx + 1 < n ? rrs[idx + 1] : n;
                        // ans += rs[i];
                    }else{
                        ans += ls[i];
                    }
                }
            }else{
                int pos = ks[i];
                int idx = lls[pos];
                if(idx >= 0){
                    // lls[idx] = lls[idx] - 1 >= 0 ? lls[lls[idx] - 1] : -1;
                    bool ok = false;
                    while(!ok && idx >= 0){
                        ok = idx == lls[idx];
                        lls[idx] = idx - 1 >= 0 ? lls[idx - 1] : -1;
                        idx = lls[idx];
                    }
                    ans += ok ? ls[i] : rs[i];
                    // if(idx >= 0 && lls[idx] != idx){
                    //     lls[idx] = -1;
                    // }
                    // // lls[idx] = idx - 1 >= 0 ? lls[idx - 1] : -1;
                    // // lls[pos] = idx - 1 >= 0 ? lls[idx - 1] : -1;
                    // ans += ls[i];
                }else{
                    ans += rs[i];
                }
            }
            // printde(i, ls[i], rs[i], ans);
            // printde(lls);
            // printde(rrs);
        }
        cout << ans << '\n';
    }
}


Measured memory_bytes: 7592