#include <bits/stdc++.h>
using namespace std;
struct camel {
  int k, l, r;
};
bool comp(camel a, camel b) { return a.l - a.r > b.l - b.r; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int test;
  cin >> test;
  while (test--) {
    int n;
    cin >> n;
    camel line[n];
    int x = 0, y = 0;
    for (__typeof(n) i = 0; i < n; i++) {
      cin >> line[i].k >> line[i].l >> line[i].r;
      if (line[i].l > line[i].r)
        x++;
      else
        y++;
    }
    sort(line, line + n, comp);
    int ans = 0;
    set<int> s;
    for (__typeof(x) i = 0; i < x; i++) s.insert(i + 1);
    set<int>::iterator it;
    for (__typeof(x) i = 0; i < x; i++) {
      it = upper_bound(s.begin(), s.end(), line[i].k);
      if (it != s.begin()) {
        it--;
        s.erase(it);
        ans += line[i].l;
      } else {
        ans += line[i].r;
        it = s.end();
        it--;
        s.erase(it);
      }
    }
    s.clear();
    for (__typeof(y) i = 0; i < y; i++) s.insert(i + 1);
    for (__typeof(n - 1) i = n - 1; i > n - 1 - y; i--) {
      it = upper_bound(s.begin(), s.end(), n - line[i].k);
      if (it != s.begin()) {
        it--;
        s.erase(it);
        ans += line[i].r;
      } else {
        ans += line[i].l;
        it = s.end();
        it--;
        s.erase(it);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
