#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long ans = 0;
  vector<long long> k(n), l(n), r(n);
  vector<long long> L, R;
  for (long long i = 0; i < n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    long long mini = min(l[i], r[i]);
    ans += mini;
    l[i] -= mini;
    r[i] -= mini;
    if (l[i] > r[i]) {
      L.push_back(i);
    } else if (l[i] < r[i]) {
      R.push_back(i);
    }
  }
  if (long long(L.size()) > 0) {
    sort((L).begin(), (L).end(), [&](long long i, long long j) {
      if (k[i] == k[j]) {
        return l[i] > l[j];
      }
      return k[i] < k[j];
    });
    long long li = 1;
    for (long long i = 0; i < long long(L.size()); i++) {
      long long j = L[i];
      if (li <= k[j]) {
        ans += l[j];
        li++;
      }
    }
  }
  if (long long(R.size()) > 0) {
    sort((R).begin(), (R).end(), [&](long long i, long long j) {
      if (k[i] == k[j]) {
        return r[i] > r[j];
      }
      return k[i] > k[j];
    });
    long long ri = n;
    for (long long i = 0; i < long long(R.size()); i++) {
      long long j = R[i];
      if (k[j] < ri) {
        ans += r[j];
        ri--;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
