
// Problem : E - Camel Train
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#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;

//Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int,int>
#define mii map<pii,int>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

//Solution

int n;

bool cmp(pair<pii,int> a, pair<pii,int> b){
    if(a.se == n) return false;
    if(b.se == n) return true;
    return abs(a.fi.fi-a.fi.se) > abs(b.fi.fi-b.fi.se);
}

void solve(){
    cin>>n;
    vector<pair<pii,int>> p(n);
    for(int i=0;i<n;i++) cin>>p[i].se>>p[i].fi.fi>>p[i].fi.se;
    sort(all(p),cmp);
    vi vis(n);
    int ans = 0;
    for(int i=0;i<n;i++){
        int l = p[i].fi.fi, r = p[i].fi.se, k = p[i].se;
        if(l >= r){
            int j = k-1;
            while(j>=0 && vis[j]) j--;
            if(j == -1){
                j = k;
                while(j<n && vis[j]) j++;
            }
            if(j >= k) ans += r;
            else ans += l;
            vis[j] = 1;
        } else {
            int j = k;
            while(j<n && vis[j]) j++;
            if(j == n){
                j = k-1;
                while(j>=0 && vis[j]) j--;
            }
            if(j >= k) ans += r;
            else ans += l;
            vis[j] = 1;
        }
    }
    cout<<ans<<'\n';
}

signed main(){
    io;
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int tests;
    cin>>tests;
    //tests = 1;
    while(tests--){
        solve();
    }
    return 0;
}