#include <bits/stdc++.h>
using namespace std;
int T;
int n;
int k[200005];
pair<int, int> lr[200005];
vector<int> lrm, lrl;
set<int> mset;
set<int, greater<int> > lset;
int ans;
bool mcomp(int l, int r) {
  return (lr[l].first - lr[l].second) > (lr[r].first - lr[r].second);
}
bool lcomp(int l, int r) {
  return (lr[l].first - lr[l].second) < (lr[r].first - lr[r].second);
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    ans = 0;
    lrm.clear();
    lrl.clear();
    mset.clear();
    lset.clear();
    for (int i = 0; i < n; i++) {
      int l, r;
      scanf("%d%d%d", &k[i], &l, &r);
      lr[i] = pair<int, int>(l, r);
      if (l >= r)
        lrm.push_back(i);
      else
        lrl.push_back(i);
    }
    sort(lrm.begin(), lrm.end(), mcomp);
    sort(lrl.begin(), lrl.end(), lcomp);
    for (int i = 1; i <= n; i++) {
      mset.insert(i);
      lset.insert(i);
    }
    for (int i : lrm) {
      auto p = mset.upper_bound(k[i]);
      if (p == mset.begin()) {
        ans += lr[i].second;
      } else {
        ans += lr[i].first;
        mset.erase(--p);
      }
    }
    for (int i : lrl) {
      auto p = lset.lower_bound(k[i]);
      if (p == lset.begin()) {
        ans += lr[i].first;
      } else {
        ans += lr[i].second;
        lset.erase(--p);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
