#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
using P = pair<T, U>;
template <class T>
using Heap = priority_queue<T>;
template <class T>
using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U>
using umap = unordered_map<T, U>;
template <class T>
using uset = unordered_set<T>;
template <class T>
bool ChangeMax(T& a, const T& b) {
  if (a >= b) return false;
  a = b;
  return true;
}
template <class T>
bool ChangeMin(T& a, const T& b) {
  if (a <= b) return false;
  a = b;
  return true;
}
template <class T, size_t N, class U>
void Fill(T (&a)[N], const U& v) {
  fill((U*)a, (U*)(a + N), v);
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& e : v) is >> e;
  return is;
}
int main() {
  int t;
  cin >> t;
  for (int slkfsadfa = 0; slkfsadfa < t; ++slkfsadfa) {
    int n;
    cin >> n;
    vector<int> k(n), l(n), r(n);
    for (int i = 0; i < n; ++i) cin >> k[i] >> l[i] >> r[i];
    vector<int> idx(n);
    for (int i = 0; i < n; ++i) idx[i] = i;
    sort(idx.begin(), idx.end(),
         [&](int i, int j) { return abs(l[i] - r[i]) > abs(l[j] - r[j]); });
    queue<int> L, R, K;
    for (int i = 0; i < n; ++i) {
      L.push(l[idx[i]]);
      R.push(r[idx[i]]);
      K.push(k[idx[i]]);
    }
    set<int> unused;
    for (int i = 0; i < n; ++i) unused.insert(i);
    ll score = 0;
    int count = 0;
    while (!L.empty()) {
      int ll = L.front();
      L.pop();
      int rr = R.front();
      R.pop();
      int kk = K.front();
      K.pop();
      if (ll - rr > 0) {
        auto it = unused.lower_bound(kk);
        if (it != unused.begin()) --it;
        if (*it < kk) {
          unused.erase(*it);
          score += ll;
        } else {
          if (count < n) {
            L.push(ll);
            R.push(rr);
            K.push(kk);
          } else {
            score += rr;
          }
        }
      } else {
        auto it = unused.lower_bound(kk);
        if (it != unused.end()) {
          unused.erase(*it);
          score += rr;
        } else {
          if (count < n) {
            L.push(ll);
            R.push(rr);
            K.push(kk);
          } else {
            score += ll;
          }
        }
      }
      count++;
    }
    cout << score << endl;
  }
  return 0;
}
