#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
T ceil(T a, T b) {
  return a / b + !!(a % b);
}
ll t;
template <typename T>
struct BIT {
  ll n;
  vector<T> d;
  BIT(ll n = 0) : n(n), d(n + 1) {}
  void add(ll i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(ll i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(ll l, ll r) { return sum(r - 1) - sum(l - 1); }
};
ll solve(vector<tuple<ll, ll, ll>> a) {
  ll ans = 0;
  sort(a.begin(), a.end());
  BIT<ll> bit(2e5 + 10);
  ll n = a.size();
  for (ll i = 0; i < (n); ++i) {
    ll k, l, r;
    tie(k, l, r) = a[i];
    l = -l, r = -r;
    if (k > n) k -= n;
    if (bit.sum(i) < k)
      ans += l;
    else
      ans += r;
    bit.add(i, 1);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  for (ll i = 0; i < (t); ++i) {
    ll n;
    cin >> n;
    vector<tuple<ll, ll, ll>> a(n);
    for (ll j = 0; j < (n); ++j) {
      ll k, l, r;
      cin >> k >> l >> r;
      if (r > l) {
        k += n;
      }
      a[j] = make_tuple(k, -l, -r);
    }
    cout << solve(a) << endl;
  }
  return 0;
}
