#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\t";
  err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
  c << "(" << v.first << "," << v.second << ")";
  return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
  out << "{ ";
  for (auto& x : c) out << x << " ";
  out << "}";
  return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct FenwickTree {
  vector<int> bit;
  int n;
  FenwickTree(int n) {
    this->n = n;
    bit.assign(n, 0);
  }
  FenwickTree(vector<int> a) : FenwickTree(a.size()) {
    for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
  }
  int sum(int r) {
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
    return ret;
  }
  int sum(int l, int r) { return sum(r) - sum(l - 1); }
  void add(int idx, int delta) {
    for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<long long, long long> > v;
    vector<pair<long long, long long> > rem;
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      k--;
      if (l > r)
        v.push_back(make_pair(l - r, k));
      else
        rem.push_back(make_pair(l - r, k));
    }
    sort(v.begin(), v.end(), greater<pair<long long, long long> >());
    FenwickTree f(n);
    for (auto& r : v) {
      if (f.sum(0, r.second) < r.second + 1) {
        f.add(r.second, 1);
        ans += r.first;
      }
    }
    FenwickTree f2(n);
    for (auto& r : rem) {
      int le = r.second + 1, ri = n - 1;
      if (le <= ri && f2.sum(le, ri) < ri - le + 1) {
        f2.add(le, 1);
      } else
        ans += r.first;
    }
    cout << ans << '\n';
  }
  return 0;
}
