#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const long long MOD = 7;
long long _gcd(long long a, long long b) {
  if (b == 0) return a;
  return _gcd(b, a % b);
}
long long gcd(long long a, long long b) {
  a = abs(a), b = abs(b);
  if (a < b) swap(a, b);
  return _gcd(a, b);
}
long long qpow(long long a, long long n) {
  long long rt = 1;
  while (n) {
    if (n & 1) rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
long long factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
long long C(long long n, long long k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  const int MIN = (1 << 31);
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> cml;
    set<int> pos;
    long long ans = 0;
    for (int u = 0; u < n; u++) {
      int k, l, r;
      cin >> k >> l >> r;
      cml.push_back({l - r, k});
      ans += r;
      pos.insert(u + 1);
    }
    sort(cml.begin(), cml.end());
    reverse(cml.begin(), cml.end());
    vector<int> csm;
    for (int u = 0; u < n; u++) {
      if (cml[u].first < 0) break;
      if (*pos.begin() <= cml[u].second) {
        auto p = pos.upper_bound(cml[u].second);
        p--;
        csm.push_back(*p);
        ans += cml[u].first;
        pos.erase(p);
        cml[u].first = MIN;
      }
    }
    sort(csm.begin(), csm.end());
    for (int v : csm) {
      if (*pos.begin() <= v) {
        pos.erase(pos.begin());
        pos.insert(v);
      }
    }
    sort(cml.begin(), cml.end());
    for (int u = 0; u < n; u++) {
      if (cml[u].first == MIN) continue;
      auto p = pos.upper_bound(cml[u].second);
      if (p == pos.end())
        ans += cml[u].first;
      else
        pos.erase(p);
    }
    cout << ans << "\n";
  }
}
int main() { solve(); }
