#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
struct cell {
  long long k, l, r;
};
struct forset {
  bool operator()(const cell &a, const cell &b) const {
    return a.l - a.r < b.l - b.r;
  }
};
long long n;
vector<cell> a, b, c;
bool cmp1(cell a, cell b) { return a.k < b.k; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    a.clear();
    b.clear();
    c.clear();
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (r >= l)
        c.push_back({k, l, r});
      else
        a.push_back({k, l, r});
    }
    sort(c.begin(), c.end(), cmp1);
    sort(a.begin(), a.end(), cmp1);
    long long cur = 0;
    multiset<cell, forset> q;
    for (long long i = 1; i <= n; i++) {
      while (cur < a.size() && a[cur].k <= i) cur++, q.insert(a[cur - 1]);
      if (q.size() > i) {
        while (q.size() != i) {
          b.push_back(*q.begin());
          q.erase(q.begin());
        }
      }
    }
    sort(b.begin(), b.end(), cmp1);
    vector<cell> all;
    for (auto x : q) all.push_back(x);
    sort(all.begin(), all.end(), cmp1);
    for (auto x : b) all.push_back(x);
    for (auto x : c) all.push_back(x);
    long long ans = 0;
    for (long long i = 0; i < all.size(); i++)
      if (all[i].k >= i + 1)
        ans += all[i].l;
      else
        ans += all[i].r;
    cout << ans << "\n";
  }
  return 0;
}
