//#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
//#include <stack>
#include <cstring>
#include <cassert>

using namespace std;

#ifdef LOCAL_DEBUG
#include <local_debug.h>
#define DEBUG(...) DBG2::print(#__VA_ARGS__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;

struct Camel {
   int K, L, R;
   friend ostream& operator<<(ostream& os, const Camel& c) {
      return os << "Camel " << c.K << ' ' << c.L << ' ' << c.R;
   }
};

namespace Greedy1 {

class BinaryIndexedTree {
   int N;
   vector<int> tree;
public:
   BinaryIndexedTree(int _N) : N(_N), tree(_N+1) {}

   int query(int idx) {
      assert(idx <= N);
      int res = 0;
      for (int i = idx; i > 0; i -= (i & -i))
         res += tree[i];
      return res;
   }

   int query(int L, int R) {
      return L <= R ? query(R) - query(L-1) : 0;
   }

   void update(int idx, int val = 1) {
      for (int i = idx; i <= N; i += (i & -i))
         tree[i] += val;
   }
};

llong solve(int N, vector<Camel> camels) {
   sort(camels.begin(), camels.end(),
       [&] (Camel a, Camel b) {
          return abs(a.L-a.R) > abs(b.L-b.R);
       }
   );

   BinaryIndexedTree bitL(N+1);
   BinaryIndexedTree bitR(N+1);
   llong res = 0;
   for (Camel c : camels) {
      if (c.L > c.R) {
         if (bitL.query(c.K) < c.K) {
         // cerr << "Place camel " << c.K << ' ' << c.L << ' ' << c.R << " left" << endl;
            bitL.update(c.K);
            res += c.L;
         }
         else {
            assert( bitR.query(c.K+1, N) < N-c.K );
         // cerr << "Place camel " << c.K << ' ' << c.L << ' ' << c.R << " right (forced)" << endl;
            bitR.update(c.K+1);
            res += c.R;
         }
      }
      else {
         if (bitR.query(c.K+1, N) < N-c.K) {
         // cerr << "Place camel " << c.K << ' ' << c.L << ' ' << c.R << " right" << endl;
            bitR.update(c.K+1);
            res += c.R;
         }
         else {
            assert( bitL.query(c.K) < c.K );
         // cerr << "Place camel " << c.K << ' ' << c.L << ' ' << c.R << " left (forced)" << endl;
            bitL.update(c.K);
            res += c.L;
         }
      }
   }

   return res;
}

}

namespace Greedy3 {

bool trace;

llong solve(int N, vector<Camel> camels) {
   llong base = 0;

   vector<Camel> camels_left, camels_right;
   for (Camel c : camels) {
      if (c.L > c.R) {
         camels_left.push_back(c);
      }
      else if (c.L < c.R)
         camels_right.push_back(c);
      base += min(c.L, c.R);
   }

   if (trace) {
      DEBUG(base);
      DEBUG(camels_left);
      DEBUG(camels_right);
   }

   sort(camels_left.begin(), camels_left.end(),
        [&] (Camel a, Camel b) {
           if (a.K != b.K)
              return a.K < b.K;
           return a.L-a.R > b.L-b.R;
        });

   sort(camels_right.begin(), camels_right.end(),
        [&] (Camel a, Camel b) {
           if (a.K != b.K)
              return a.K > b.K;
           return a.R-a.L > b.R-b.L;
        });

   llong res = base;

   {
      if (trace) cerr << "Left" << endl;
      priority_queue< int, VI, greater<int> > pq_left;
      for (Camel c : camels_left) {
         if (trace) cerr << c << endl;
         if (c.K > SZ(pq_left)) {
            pq_left.push(c.L-c.R);
         }
         else {
            pq_left.push(c.L-c.R);
            pq_left.pop();
         }
      }

      while (!pq_left.empty()) {
         if (trace) cerr << "+ " << pq_left.top() << endl;
         res += pq_left.top();
         pq_left.pop();
      }
   }


   {
   // cerr << "Right" << endl;
      priority_queue< int, VI, greater<int> > pq_right;

      for (Camel c : camels_right) {
         if (trace) cerr << c << endl;
         if (N-c.K > SZ(pq_right)) {
            pq_right.push(c.R-c.L);
         }
         else {
            pq_right.push(c.R-c.L);
            pq_right.pop();
         }
      }

      while (!pq_right.empty()) {
         if (trace) cerr << "+ " << pq_right.top() << endl;
         res += pq_right.top();
         pq_right.pop();
      }
   }

   return res;
}
}

int main(int argc, char* argv[]) {
   ios_base::sync_with_stdio(false); 
   cin.tie(nullptr);

   int TC;
   cin >> TC;
   FOR(tc, 1, TC) {
      int N;
      cin >> N;
      vector<Camel> camels;
      REP(i, N) {
         int K, L, R;
         cin >> K >> L >> R;
         camels.push_back({K, L, R});
      }
      llong res = Greedy3::solve(N, camels);
      cout << res << '\n';
   }

   return 0;
}


Measured memory_bytes: 13196