#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a) out << it << " ";
  return out;
}
template <class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A>
istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a) in >> i;
  return in;
}
long long poww(const long long &a, long long b,
               const long long &m = 1000000007) {
  if (b == 0) return 1;
  long long x = poww(a, b / 2, m);
  x = x * x % m;
  if (b & 1) x = x * a % m;
  return x;
}
long long ceil(const long long &a, const long long &b) {
  return (a + b - 1) / b;
}
long long f(const vector<pair<long long, long long> > &v) {
  long long lst = -1;
  long long spaces = 0;
  multiset<long long> add;
  for (auto c : v) {
    spaces += c.first - lst;
    lst = c.first;
    if (spaces > 0) {
      add.insert(c.second);
      spaces--;
    } else {
      if (!add.empty() && *(--add.end()) < c.second) {
        add.erase(--add.end());
        add.insert(c.second);
      }
    }
  }
  long long ans = 0;
  for (long long i : add) ans += i;
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    vector<pair<long long, long long> > L, R;
    long long n, ans = 0;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      k--;
      if (l >= r)
        L.push_back({k, l - r});
      else
        R.push_back({n - (k + 1) - 1, r - l});
      ans += min(l, r);
    }
    sort((L).begin(), (L).end(),
         [](pair<long long, long long> a, pair<long long, long long> b) {
           if (a.first != b.first) return a.first < b.first;
           return a.second > b.second;
         });
    sort((R).begin(), (R).end(),
         [](pair<long long, long long> a, pair<long long, long long> b) {
           if (a.first != b.first) return a.first < b.first;
           return a.second > b.second;
         });
    cout << ans + f(R) + f(L) << "\n";
  }
}
