#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <typename T>
void debug(vector<vector<T>> &v, long long h, long long w) {
  for (long long i = 0; i < h; i++) {
    cerr << v[i][0];
    for (long long j = 1; j < w; j++) cerr << " " << v[i][j];
    cerr << endl;
  }
};
template <typename T>
void debug(vector<T> &v, long long n) {
  if (n != 0) cerr << v[0];
  for (long long i = 1; i < n; i++) cerr << " " << v[i];
  cerr << endl;
};
long long solve(vector<long long> &K, vector<long long> &L,
                vector<long long> &R) {
  long long N = K.size();
  vector<vector<long long>> G(N);
  for (long long i = 0; i < (long long)N; i++) {
    long long idx = min(K[i], N - 1);
    G[idx].push_back(i);
  }
  priority_queue<pair<long long, long long>> pq;
  long long res = 0;
  for (long long i = N - 1; i >= 0; i--) {
    for (auto idx : G[i]) {
      pair<long long, long long> tmp = {L[idx], idx};
      pq.push(tmp);
    }
    if (not pq.empty()) {
      auto p = pq.top();
      pq.pop();
      res += p.first;
    }
  }
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    res += R[p.second];
  }
  return res;
}
void solve() {
  long long N;
  cin >> N;
  vector<long long> K(N), L(N), R(N);
  for (long long i = 0; i < (long long)N; i++) cin >> K[i] >> L[i] >> R[i];
  for (long long i = 0; i < (long long)N; i++) K[i]--;
  vector<long long> l1, r1, k1, l2, r2, k2;
  long long res = 0;
  for (long long i = 0; i < (long long)N; i++) {
    if (K[i] == N - 1) {
      res += L[i];
    } else if (L[i] >= R[i]) {
      l1.push_back(L[i]);
      r1.push_back(R[i]);
      k1.push_back(K[i]);
    } else {
      l2.push_back(R[i]);
      r2.push_back(L[i]);
      k2.push_back(N - 2 - K[i]);
    }
  }
  res += solve(k1, l1, r1);
  res += solve(k2, l2, r2);
  cout << res << endl;
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long T;
  cin >> T;
  while (T--) solve();
  return 0;
}
