#include <bits/stdc++.h>
using namespace std;
const long long INF = (1 << 30) - 1;
const long long LLINF = (1LL << 60LL);
const long long MOD = 1000000007;
const long long MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
long long dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
long long dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void solve() {
  long long n;
  long long k[200010], l[200010], r[200010], d[200010];
  vector<pair<long long, pair<long long, long long>>> v, w;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    d[i] = l[i] - r[i];
    if (d[i] >= 0)
      v.push_back(make_pair(k[i], pair<long long, long long>(d[i], l[i])));
    else
      w.push_back(make_pair(k[i], pair<long long, long long>(-d[i], r[i])));
  }
  sort(v.begin(), v.end());
  sort(w.begin(), w.end());
  reverse(w.begin(), w.end());
  long long now = 0, now2 = 0;
  long long ans = 0;
  for (long long i = 1; i < n + 1; i++) {
    priority_queue<long long, vector<long long>, greater<long long>> que;
    for (long long j = now; j < (long long)v.size(); j++) {
      if (v[j].first == i) {
        que.push(v[j].second.first);
        ans += v[j].second.second;
        now++;
      } else
        break;
    }
    while ((long long)que.size() > i) {
      long long a = que.top();
      que.pop();
      ans -= a;
    }
  }
  for (long long i = n; i >= 1; i--) {
    priority_queue<long long, vector<long long>, greater<long long>> que;
    for (long long j = now2; j < (long long)w.size(); j++) {
      if (w[j].first == i) {
        que.push(w[j].second.first);
        ans += w[j].second.second;
        now2++;
      } else
        break;
    }
    while ((long long)que.size() > (n - i)) {
      long long a = que.top();
      que.pop();
      ans -= a;
    }
  }
  cout << ans << endl;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) solve();
  return 0;
}
