#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e+18 + 1;
const long long MOD = 1000000007LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vector<long long> dx = {0, 0, 1, -1};
vector<long long> dy = {1, -1, 0, 0};
map<long long, long long> prime_factor(long long n) {
  map<long long, long long> res;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1) res[n] = 1;
  return res;
}
const long long MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(long long n, long long k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  long long t;
  cin >> t;
  for (long long time = 0; time < (long long)(t); time++) {
    long long n;
    cin >> n;
    long long ans = 0;
    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];
    }
    vector<long long> diff(n);
    for (long long i = 0; i < (long long)(n); i++) {
      ans += r[i];
      diff[i] = l[i] - r[i];
    }
    vector<vector<long long> > vec(n);
    for (long long i = 0; i < (long long)(n); i++) {
      vec[k[i] - 1].push_back(diff[i]);
    }
    priority_queue<long long, vector<long long>, greater<long long> > S;
    for (long long i = 0; i < (long long)(n); i++) {
      for (auto x : vec[i]) {
        if (S.size() < i + 1) {
          if (S.top() == x) {
            S.pop();
            S.push(x);
          }
        } else {
          S.push(x);
        }
      }
      while (S.size() < i + 1) S.push(0);
    }
    while (S.size() > 0) {
      ans += S.top();
      S.pop();
    }
    cout << ans << endl;
  }
}
