#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;
const int SZ = 200099;
int T, N, K[SZ], L[SZ], R[SZ], M[SZ];
long long happiness(vector<int> A, bool type) {
  int AS = A.size();
  if (!type) {
    long long ans = 0;
    vector<int> B(AS + 1, INT_MAX), V(N + 1, 0);
    for (auto in : A) {
      K[in] = min(K[in], AS);
      B[K[in]] = K[in];
    }
    for (int i = AS - 1; i >= 1; i--) {
      if (B[i] == INT_MAX && B[i + 1] != INT_MAX) B[i] = B[i + 1];
    }
    vector<int> bydiff = A;
    vector<int> byk = A;
    sort(bydiff.begin(), bydiff.end(), [](const int &i, const int &j) {
      int diffi = (L[i] - R[i]);
      int diffj = (L[j] - R[j]);
      if (diffi == diffj) return K[i] > K[j];
      return diffi > diffj;
    });
    sort(byk.begin(), byk.end(),
         [](const int &i, const int &j) { return K[i] < K[j]; });
    int i = AS - 1, j = AS - 1, k = AS;
    while (k > 0) {
      if (V[byk[j]]) {
        j--;
        continue;
      }
      if (V[bydiff[i]]) {
        i--;
        continue;
      }
      if (K[byk[j]] >= B[k]) {
        ans += L[byk[j]];
        V[byk[j]] = 1;
      } else if (K[bydiff[i]] >= B[k]) {
        ans += L[bydiff[i]];
        V[bydiff[i]] = 1;
      } else {
        ans += R[bydiff[i]];
        V[bydiff[i]] = 1;
      }
      k--;
    }
    return ans;
  }
  return -1;
}
long long max_happiness() {
  vector<int> A, B;
  for (int i = 1; i <= N; i++) {
    if (L[i] >= R[i]) {
      A.push_back(i);
    } else {
      B.push_back(i);
    }
  }
  for (auto in : B) {
    swap(L[in], R[in]);
    K[in] = N - K[in];
    if (K[in] == 0) {
      L[in] = R[in];
      K[in] = N;
    }
  }
  return happiness(A, false) + happiness(B, false);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> T;
  while (T--) {
    cin >> N;
    for (int i = 0; i < N; i++) cin >> K[i + 1] >> L[i + 1] >> R[i + 1];
    cout << max_happiness() << endl;
  }
  return 0;
}
