#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int inf = long long int(1e18);
const double PI = acos(-1);
long long int modexp(long long int x, long long int n, int md) {
  if (n == 0) return 1;
  if (n % 2 == 0) return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}
vector<int> g[200001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  long long int t, n, k, l, r;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<pair<long long int, pair<long long int, long long int>>> a;
    for (int i = 1; i < n + 1; ++i) g[i].clear();
    for (int i = 0; i < n; ++i) {
      cin >> k >> l >> r;
      a.push_back({k, {l, r}});
      g[k].push_back(i);
    }
    long long int ans = 0;
    set<pair<long long int, long long int>,
        greater<pair<long long int, long long int>>>
        av1, av2;
    for (int i = 0; i < n; ++i) {
      av1.insert({a[i].second.second - a[i].second.first, a[i].second.second});
    }
    for (int i = n; i >= 1; --i) {
      for (auto e : g[i]) {
        if (av1.find(make_pair(a[e].second.second - a[e].second.first,
                               a[e].second.second)) != av1.end()) {
          av1.erase(
              {a[e].second.second - a[e].second.first, a[e].second.second});
          av2.insert(
              {a[e].second.first - a[e].second.second, a[e].second.first});
        }
      }
      if (!av1.empty()) {
        auto ptr = *av1.begin();
        if (ptr.first > 0 or av2.empty()) {
          av1.erase(ptr);
          ans += ptr.second;
          continue;
        }
      }
      auto ptr = *av2.begin();
      av2.erase(ptr);
      ans += ptr.second;
    }
    cout << ans << "\n";
  }
}
