#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7, MAX = 1e6 + 5;
const long long INF = 1e18 + 5;
long long powN(long long a, long long p) {
  if (p == 0) return 1;
  long long z = powN(a, p / 2);
  z = (z * z) % MOD;
  if (p % 2) z = (z * a) % MOD;
  return z;
}
vector<long long> Fac(MAX), Inv(MAX);
void CalcFac() {
  Fac[0] = Inv[0] = 1;
  for (int i = 1; i < MAX; i++) {
    Fac[i] = (Fac[i - 1] * i) % MOD;
    Inv[i] = powN(Fac[i], MOD - 2);
  }
}
long long NCR(int n, int r) {
  if (r > n) return 0;
  return (((Fac[n] * Inv[r]) % MOD) * Inv[n - r]) % MOD;
}
vector<bool> is_prime(MAX, true);
vector<int> MinDiv(MAX);
void Sieve() {
  is_prime[0] = is_prime[1] = false;
  int i, j;
  for (i = 2; i * i <= MAX; i++) {
    if (is_prime[i]) {
      MinDiv[i] = i;
      for (j = i * i; j <= MAX; j += i) {
        is_prime[j] = false;
        MinDiv[j] = i;
      }
    }
  }
  for (int i = 2; i <= MAX; i++)
    if (is_prime[i]) MinDiv[i] = i;
}
bool cmp(vector<int> X, vector<int> Y) {
  if (X[3] == Y[3]) {
    return X[0] > Y[0];
  }
  return X[3] > Y[3];
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int> > X(n, vector<int>(4));
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      X[i][0] = k;
      X[i][1] = l;
      X[i][2] = r;
      X[i][3] = l - r;
    }
    sort(X.begin(), X.end(), cmp);
    set<int> S;
    for (int i = 1; i <= n; i++) S.insert(i);
    long long ans = 0;
    for (auto x : X) {
      if (x[3] > 0) {
        auto pos = S.upper_bound(x[0]);
        pos--;
        if (*pos > x[0]) {
          ans += x[2];
        } else {
          ans += x[1];
          S.erase(*pos);
        }
      } else {
        auto pos = S.rbegin();
        if (*pos > x[0]) {
          ans += x[2];
        } else {
          ans += x[1];
          S.erase(*pos);
        }
      }
    }
    cout << ans << endl;
  }
}
