#include <bits/stdc++.h>
using namespace std;
struct ppp {
  long long k, l, r;
};
bool cmp(ppp x, ppp y) {
  if (x.k == y.k) return x.l - x.r > y.l - y.r;
  return x.k < y.k;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long n, ans = 0;
    cin >> n;
    vector<ppp> p, c;
    for (long long i = 1; i <= n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (l > r) p.push_back({k, l, r});
      if (l < r) c.push_back({k, l, r});
      if (l == r) ans += l;
    }
    sort(p.begin(), p.end(), cmp);
    sort(c.begin(), c.end(), cmp);
    int now = 1;
    for (int i = 0; i < p.size(); i++) {
      if (now <= p[i].k)
        ans += p[i].l, now++;
      else
        ans += p[i].r;
    }
    int last = n;
    for (int i = c.size() - 1; i >= 0; i--) {
      if (last > c[i].k)
        ans += c[i].r, last--;
      else
        ans += c[i].l;
    }
    cout << ans << endl;
  }
}
