#include <bits/stdc++.h>
using namespace std;
using VP = vector<pair<long long, long long> >;
using VVP = vector<VP>;
using VI = vector<long long>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const long long INF = 1LL << 61;
const long long mod = 1e9 + 7;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void solve() {
  int i, j;
  int n;
  cin >> n;
  vector<pair<long long, long long> > v;
  long long ans = 0;
  for (i = 0; i < n; i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    ans += r;
    if (l - r > 0)
      v.push_back(pair<long long, long long>(l - r, k));
    else
      v.push_back(pair<long long, long long>(r - l, -k));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  set<long long> s;
  for (i = 1; i <= n; i++) s.insert(i);
  for (i = 0; i < n; i++) {
    if (v[i].second > 0) {
      auto it = s.lower_bound(1 + v[i].second);
      if (it == s.end()) {
        ans += v[i].first;
        continue;
      }
      if (it == s.begin()) continue;
      it--;
      ans += v[i].first;
      s.erase(it);
    } else {
      auto it = s.lower_bound(1 + -v[i].second);
      if (it == s.begin()) {
        continue;
      }
      if (it == s.end()) {
        ans -= v[i].first;
      } else {
        s.erase(it);
      }
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  int t;
  cin >> t;
  while (t--) solve();
}
