#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, m;
vector<pair<int, pair<int, int>>> v[N], v1[N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    scanf("%d", &n);
    int k, l, r;
    long long ans = 0;
    for (int i = 0; i <= n + 1; ++i) {
      v[i].clear();
      v1[i].clear();
    }
    set<pair<int, pair<int, int>>> s;
    for (int i = 0; i < n; ++i) {
      scanf("%d%d%d", &k, &l, &r);
      if (r >= l) {
        v1[k + 1].push_back(make_pair(-(r - l), make_pair(-r, -l)));
      } else {
        v[k].push_back(make_pair(-(l - r), make_pair(-l, -r)));
      }
    }
    for (int i = n; i >= 1; --i) {
      for (auto it : v[i]) s.insert(it);
      if (s.size() > 0) {
        ans -= s.begin()->second.first;
        s.erase(s.begin());
      }
    }
    while (s.size() > 0) {
      ans -= s.begin()->second.second;
      s.erase(s.begin());
    }
    for (int i = 1; i <= n; ++i) {
      for (auto it : v1[i]) s.insert(it);
      if (s.size() > 0) {
        ans -= s.begin()->second.first;
        s.erase(s.begin());
      }
    }
    while (s.size() > 0) {
      ans -= s.begin()->second.second;
      s.erase(s.begin());
    }
    for (auto it : v1[n + 1]) ans -= it.second.second;
    printf("%lld\n", ans);
  }
}
