#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1LL << 61;
struct query {
  long long type;
  long long value;
  query(long long a = 0, long long b = 0) : type(a), value(b) {}
};
long long INF = (1LL << 60);
struct segtree {
  long long SIZE;
  vector<query> s;
  vector<long long> t;
  vector<long long> u;
  segtree(long long n = 1) {
    SIZE = 1;
    while (SIZE < n) SIZE *= 2;
    s.clear();
    t.clear();
    u.clear();
    s.resize(SIZE * 2, query());
    t.resize(SIZE * 2, 0);
    u.resize(SIZE * 2, 0);
  }
  void func(long long k, long long l, long long r, query q) {
    if (q.type == 1) {
      if (s[k].type == 0)
        s[k] = q;
      else
        s[k].value += q.value;
      t[k] += q.value * (r - l);
      u[k] += q.value;
    }
    if (q.type == 2) {
      s[k] = q;
      t[k] = q.value * (r - l);
      u[k] = q.value;
    }
  }
  void compute(long long k, long long l, long long r) {
    query q = s[k];
    s[k] = query();
    if (q.type == 0 || r - l == 1) return;
    long long m = (l + r) / 2;
    func(k * 2 + 1, l, m, q);
    func(k * 2 + 2, m, r, q);
  }
  void Update(long long a, long long b, query x, long long k, long long l,
              long long r) {
    if (b <= l || r <= a) return;
    compute(k, l, r);
    if (a <= l && r <= b) {
      func(k, l, r, x);
    } else {
      long long m = (l + r) / 2;
      Update(a, b, x, k * 2 + 1, l, m);
      Update(a, b, x, k * 2 + 2, m, r);
      t[k] = t[k * 2 + 1] + t[k * 2 + 2];
      u[k] = min(u[k * 2 + 1], u[k * 2 + 2]);
    }
  }
  long long Dfs(long long type, long long a, long long b, long long k,
                long long l, long long r) {
    if (b <= l || r <= a) {
      if (type == 1) return 0;
      if (type == 2) return INF;
    }
    compute(k, l, r);
    if (a <= l && r <= b) {
      if (type == 1) return t[k];
      if (type == 2) return u[k];
    } else {
      long long m = (l + r) / 2;
      long long lv = Dfs(type, a, b, k * 2 + 1, l, m);
      long long rv = Dfs(type, a, b, k * 2 + 2, m, r);
      if (type == 1) return lv + rv;
      if (type == 2) return min(lv, rv);
    }
  }
  void Add(long long a, long long b, long long x) {
    Update(a, b, query(1, x), 0, 0, SIZE);
  }
  void Set(long long a, long long b, long long x) {
    Update(a, b, query(2, x), 0, 0, SIZE);
  }
  long long Getsum(long long a, long long b) {
    return Dfs(1, a, b, 0, 0, SIZE);
  }
  long long Getmin(long long a, long long b) {
    return Dfs(2, a, b, 0, 0, SIZE);
  }
};
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long> > a, b;
    segtree T(n);
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      k--;
      ans += r;
      if (l - r > 0) {
        a.push_back({l - r, -k});
      } else if (l - r < 0) {
        b.push_back({l - r, -k});
      }
    }
    sort(a.rbegin(), a.rend());
    sort(b.begin(), b.end());
    for (long long i = 0; i < a.size(); i++) {
      long long id = -a[i].second;
      long long v = a[i].first;
      long long ng = -1, ok = n;
      while (ng + 1 < ok) {
        long long m = (ng + ok) / 2;
        if (T.Getsum(m, m + 1) <= m)
          ok = m;
        else
          ng = m;
      }
      if (ok <= id) {
        T.Add(id, n + 1, 1);
        ans += v;
      }
    }
    segtree R(n);
    long long pre = 0;
    for (long long i = 0; i < n; i++) {
      long long v = T.Getsum(i, i + 1) - pre;
      while (v--) {
        R.Add(pre, pre + 1, 1);
        pre++;
      }
    }
    for (long long i = 0; i < b.size(); i++) {
      long long id = -b[i].second;
      long long v = b[i].first;
      long long ng = n, ok = -1;
      while (ok + 1 < ng) {
        long long m = (ng + ok) / 2;
        if (R.Getsum(m, m + 1) <= n - m)
          ok = m;
        else
          ng = m;
      }
      if (id + 1 <= ok) {
        R.Add(0, id + 2, 1);
      } else
        ans += v;
    }
    cout << ans << endl;
  }
}
