#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

int TT;
void solve(int t);
void answer() {
    cin>>TT;
    REP1(t,TT) solve(t);
}
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    answer();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif

int N,Q,K;
int A[SIZE];

struct Item{
    int k,l,r;
    bool operator < (const Item& that) const {
        return abs(l-r) < abs(that.l-that.r);
    }
};


void solve(int _t) {
    cin>>N;
    int K,L,R;
    vector<Item> items;
    priority_queue<Item> que;
    set<int> s;
    // 左の方が大きい人
    vector<vector<int>> lefts(N), rights(N);
    REP(i,N) {
        cin>>K>>L>>R;
        --K;
        items.push_back({K,L,R});
        if (L>=R) {
            lefts[K].push_back(i);
        } else {
            rights[K].push_back(i);
        }
    }
    vector<Item> que2;
    int ans = 0;
    int pos = 0;
    REP(i,N) {
        for (int j:lefts[i]) que.push(items[j]);
        if (que.size()) {
            auto [k,l,r] = que.top();
            que.pop();
            if (pos <= k) {
                ans += l;
                pos++;
            } else {
                que2.push({k,l,r});
            }
        }
    }
    pos = N-1;
    RREP(i,N) {
        for (int j:rights[i]) que.push(items[j]);
        if (que.size()) {
            auto [k,l,r] = que.top();
            que.pop();
            if (pos <= k) {
                que.push({k,l,r});
            } else {
                ans += (pos <= k) ? l : r;
                pos--;

            }
        }
    }
    while(que2.size()) {
        // 右に寄せて埋める
        auto [k,l,r] = que2.top();
        que2.pop();
        ans += (pos <= k) ? l : r;
        pos--;
    }
    out("%lld\n", ans);
}


