#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << (*i) << " ";
  cout << endl;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
long long nextLong() {
  long long x;
  scanf("%lld", &x);
  return x;
}
long long solve(vector<pair<long long, long long>> A, const int N) {
  sort((A).begin(), (A).end());
  reverse((A).begin(), (A).end());
  long long res = 0;
  priority_queue<long long> pq;
  int j = 0;
  for (int i = N; i >= 0; i--) {
    while (j < A.size() && A[j].first >= i) {
      pq.push(A[j].second);
      j++;
    }
    if (!pq.empty()) {
      res += pq.top();
      pq.pop();
    }
  }
  return res;
}
int main2() {
  int N = nextLong();
  long long base = 0;
  vector<pair<long long, long long>> A, B;
  for (int i = 0, _n = (int)(N); i < _n; ++i) {
    int K = nextLong();
    int L = nextLong();
    int R = nextLong();
    if (L > R) {
      base += R;
      A.push_back({K, L - R});
    }
    if (L == R) base += L;
    if (L < R) {
      base += L;
      if (N - K > 0) B.push_back({N - K, R - L});
    }
  }
  sort((A).begin(), (A).end());
  sort((B).begin(), (B).end());
  long long ans = base + solve(A, N) + solve(B, N);
  cout << ans << endl;
  return 0;
}
int main() {
  int t = nextLong();
  while (t--) main2();
  return 0;
}
