#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
long long fun(multiset<pair<int, int>> &S) {
  long long res = 0;
  multiset<int> _S;
  for (auto it : S) {
    if (it.first) {
      _S.insert(it.second);
      while (_S.size() > it.first) {
        _S.erase(*_S.begin());
      }
    }
  }
  for (auto it : _S) res += it;
  return res;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    multiset<pair<int, int>> L, R;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      int k, l, r;
      scanf("%d %d %d", &k, &l, &r);
      if (l >= r) {
        L.insert({k, l - r});
        ans += r;
      } else {
        R.insert({n - k, r - l});
        ans += l;
      }
    }
    ans += fun(L);
    ans += fun(R);
    printf("%lld\n", ans);
  }
  return 0;
}
