#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int cur, k[maxn];
long long l[maxn], r[maxn];
vector<int> L, R;
bool Less_L(int x, int y) { return k[x] < k[y]; }
bool Less_R(int x, int y) { return k[x] > k[y]; }
set<long long> all;
void fl() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%lld%lld", &k[i], &l[i], &r[i]);
    long long ans = 0;
    L.clear();
    R.clear();
    for (int i = 1; i <= n; i++) {
      if (l[i] > r[i]) L.push_back(i);
      if (l[i] < r[i]) R.push_back(i);
      ans += min(l[i], r[i]);
    }
    sort(L.begin(), L.end(), Less_L);
    cur = 1;
    all.clear();
    for (int i = 0; i < (int)L.size(); i++) {
      int id = L[i];
      long long now = l[id] - r[id];
      if (cur > k[id]) {
        if (all.empty()) continue;
        long long last = *all.begin();
        if (last < now) {
          ans = ans - last + now;
          all.erase(all.begin());
          all.insert(now);
        }
      } else {
        ans = ans + now;
        all.insert(now);
        cur++;
      }
    }
    sort(R.begin(), R.end(), Less_R);
    cur = n;
    all.clear();
    for (int i = 0; i < (int)R.size(); i++) {
      int id = R[i];
      long long now = r[id] - l[id];
      if (cur <= k[id]) {
        if (all.empty()) continue;
        long long last = *all.begin();
        if (last < now) {
          ans = ans - last + now;
          all.erase(all.begin());
          all.insert(now);
        }
      } else {
        ans = ans + now;
        all.insert(now);
        cur--;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
