#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
class BIT {
 public:
  vector<ll> s;
  vector<bool> v;
  const ll n;
  const int Sq = 500;
  BIT(ll n_) : s(n_ + 1, 0), v(500, 0), n(n_) {}
  ll sum(ll i) {
    ll k = 0;
    while (i > 0) {
      k += s[i];
      i -= i & (-i);
    }
    return k;
  }
  ll sum(ll l, ll r) { return sum(r) - sum(l - 1); }
  void add(ll pos, ll val) {
    while (pos <= n) {
      s[pos] += val;
      pos += pos & (-pos);
    }
  }
  void Bg(int pos) {
    int Bgpos = (pos - 1) / Sq;
    for (int i = Bgpos; i >= 0; i--) {
      if (v[i])
        continue;
      else {
        for (int j = min((i + 1ll) * Sq, pos * 1ll); j >= i * Sq + 1; j--) {
          if (s[j] != 0) continue;
          add(j, 1);
          if (sum((i + 1) * Sq) - sum(i * Sq) < Sq)
            return;
          else {
            v[i] = true;
            return;
          }
        }
      }
    }
  }
};
struct cammel {
  int pos;
  ll l, r;
  bool operator<(const cammel& o) const { return (l - r) < (o.l - o.r); };
};
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    BIT bit(n);
    vector<cammel> v(n);
    for (ll i = 0; i < (ll)n; i++)
      scanf("%d %lld %lld", &v[i].pos, &v[i].l, &v[i].r);
    sort(v.rbegin(), v.rend());
    ll res = 0;
    for (ll i = 0; i < (ll)n; i++) {
      int gg = bit.sum(v[i].pos);
      if (gg >= v[i].pos)
        res += v[i].r;
      else {
        res += v[i].l;
        bit.Bg(v[i].pos);
      }
    }
    printf("%d\n", res);
  }
  return 0;
}
