#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <math.h>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
 
 
#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s
 
typedef long long ll;
 
using namespace std;

ll modd = 1000*1000*1000+7;

template<class T>
T fastpower(T a, long long n, ll modu) {  // n must be >= 1
  if (n==0) {return (1%modu);}
  if (n % 2 == 0) {  
      T halfpower = fastpower<T>(a, n/2, modu);
      return (halfpower*halfpower) % modu;
  } else {
      if (n == 1) {  return a; } else {  
          T halfpower = fastpower<T>(a, n/2, modu);
          return (((halfpower*halfpower) % modu)*a)%modu;
      }
  }
};

int solve(int rr) {
    if (rr==0) {return 0;}
    int ct = 0;
    int rr_orig(rr);
    while (rr>0) {
        ct += rr%2;
        rr/=2;
    }
    return 1+solve(rr_orig%ct);
}

int main()   {

    ios_base::sync_with_stdio(false);

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no
 
//    auto start = chrono::steady_clock::now();
  
    readint(test_cases);
//    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n);
        vector<ll> k, l, r;
        forr(i,0,n) {
            readint(kk); readint(qq); readint(rr);
            k.push_back(kk);
            l.push_back(qq);
            r.push_back(rr);
        }
        set<pair<ll,int>> candidates_left, candidates_right;
        map<int,vector<int>> k_freq_left, k_freq_right;
        forr(i,0,n) {
            if (l[i]>r[i]) {
              k_freq_left[k[i]].push_back(i);
//              candidates_left.insert(make_pair(l[i]-r[i], i));
            }
            if ((l[i]<r[i]) && (n-k[i]>0)) {
//              candidates_right.insert(make_pair(l[i]-r[i], i));
              k_freq_right[n-k[i]].push_back(i);
            }
        }

        vector<int> ans_left, ans_right;
        set<int> ans_left_set, ans_right_set;
        for(int i=n; i >= 1; --i) {
            for(auto x : k_freq_left[i]) {
                candidates_left.insert(make_pair(l[x]-r[x],x));
            }
            if (candidates_left.empty()) {continue;}
            ans_left.push_back(prev(candidates_left.end())->second);
            ans_left_set.insert(prev(candidates_left.end())->second);
            candidates_left.erase(prev(candidates_left.end()));
        }
        for(int i=n; i >= 1; --i) {
            for(auto x : k_freq_right[i]) {
                candidates_right.insert(make_pair(l[x]-r[x],x));
            }
            if (candidates_right.empty()) {continue;}
            ans_right.push_back(candidates_right.begin()->second);
            ans_right_set.insert(candidates_right.begin()->second);
            candidates_right.erase(candidates_right.begin());
        }

        ll ret = 0;
        for(auto x : ans_left) {
            ret += l[x];
        }
        for(auto x : ans_right) {
            ret += r[x];
        }
        forr(i,0,n) {
            if ((!set_map_includes(ans_left_set, i)) && (!set_map_includes(ans_right_set, i))) {
                ret += min(l[i], r[i]);
            }
        }

        cout << ret << endl;

    }
 
//    auto end = chrono::steady_clock::now();
//    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;
 
    return 0;
}


Measured memory_bytes: 53844