            #include <bits/stdc++.h>
            #include <ext/pb_ds/assoc_container.hpp>
            #include <ext/pb_ds/tree_policy.hpp>
            using namespace std;
            using namespace __gnu_pbds;
            #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
            #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
            #define endl "\n"
            #define pb push_back
            #define F first
            #define S second
            #define print(a)       for(auto x : a) cout << x << " "; cout << endl
            #define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
            #define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
            #define mk(x,y)  make_pair(x,y)
            typedef int ll;
            const ll N =  1e6+5;
            const ll mod = 1e9+7;
            ///const ll inf = 1e18;
            ll z,  n  , m , k[N] , l[N] , r[N]  , x , y  , q   ;
            vector <ll> frnt[N];
            vector <ll> bck[N];

    ll pow_mod_m(ll a , ll n)
    {
        if(!n)
            return 1;
        ll pt =  pow_mod_m(a,n/2);
        pt *= pt , pt %= mod;
        if(n&1)
            pt *= a  ,pt %= mod;
        return pt;
    }

    int main()
    {
         IOS;
         int ts =1;
         cin >> ts ;
         while(ts--)
         {
            cin >> n;
            ll ans =0 ;
            ll mxfrnt  = 0, mxbck = 0;
            rep(i,0,n)
            {
                cin >> k[i] >> l[i] >> r[i];
                if(l[i] > r[i])
                {
                    frnt[k[i]].pb(l[i] - r[i]);
                    ans += r[i];
                    mxfrnt = max(mxfrnt , k[i]);
                }
                else if(l[i] < r[i])
                {
                    bck[n - k[i]].pb(r[i] - l[i]);
                    ans += l[i];
                    mxbck = max(mxbck , n - k[i]);
                }
                else
                    ans += l[i];
            }
            priority_queue <ll>  pq;
            for(int i = mxfrnt ; i>= 1; i--)
            {
                for(auto u : frnt[i]) pq.push(u);
                if(pq.empty()) continue;
                ans += pq.top();
                pq.pop();
            }
            while(!pq.empty()) pq.pop();
            for(int i = mxbck; i>=1; i--)
            {
                for(auto u : bck[i]) pq.push(u);
                if(pq.empty()) continue;
                ans += pq.top();
                pq.pop();
            }
          ///  while(!pq.empty()) pq.pop();
            cout << ans << endl;
            rep(i,0,max(mxfrnt , mxbck)+1) {frnt[i].clear();
            bck[i].clear();
            }
         }
    }
