#include <bits/stdc++.h>
using namespace std;
void solve() {
  int N;
  cin >> N;
  long long res = 0;
  vector<pair<int, int> > A, B;
  for (int i = 0; i < N; i++) {
    int k, l, r;
    cin >> k >> l >> r;
    k--;
    int mini = min(l, r);
    res += mini;
    l -= mini;
    r -= mini;
    if (l) {
      A.push_back({l, k});
      continue;
    }
    if (r) {
      B.push_back({r, k + 1});
    }
  }
  sort((A).begin(), (A).end());
  reverse((A).begin(), (A).end());
  sort((B).begin(), (B).end());
  reverse((B).begin(), (B).end());
  set<int> S;
  int lim = A.size();
  for (int i = 0; i < lim; i++) S.insert(i);
  for (int i = 0; i < (int)A.size(); i++) {
    if (A[i].second >= lim) {
      res += A[i].first;
      continue;
    }
    auto it = S.lower_bound(A[i].second);
    if (*it == A[i].second) {
      S.erase(*it);
      res += A[i].first;
      continue;
    }
    if (it == S.begin()) continue;
    it--;
    S.erase(*it);
    res += A[i].first;
  }
  S.clear();
  lim = B.size();
  for (int i = (int)A.size(); i < (int)A.size() + lim; i++) S.insert(i);
  for (int i = 0; i < (int)B.size(); i++) {
    if (B[i].second <= (int)A.size() - 1) {
      res += B[i].first;
      continue;
    }
    auto it = S.lower_bound(B[i].second);
    if (*it == B[i].second) {
      S.erase(*it);
      res += B[i].first;
      continue;
    }
    if (it == S.end()) continue;
    S.erase(*it);
    res += B[i].first;
  }
  cout << res << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
