#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1000000007;
long long INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}
using Graph = vector<vector<tuple<long long, long long, long long>>>;
long long K[200010];
long long L[200010];
long long R[200010];
long long f[200010];
pair<long long, long long> P[200010];
int main() {
  long long T;
  cin >> T;
  for (long long i = (long long)0; i < (long long)T; i++) {
    long long N;
    cin >> N;
    for (long long i = (long long)0; i < (long long)N + 5; i++) f[i] = 0;
    for (long long i = (long long)0; i < (long long)N; i++) {
      cin >> K[i] >> L[i] >> R[i];
      P[i] = make_pair(max(L[i], R[i]), i);
    }
    sort(P, P + N);
    reverse(P, P + N);
    long long ans = 0;
    for (long long i = (long long)0; i < (long long)N; i++) {
      long long index = P[i].second;
      if (L[index] == R[index]) {
        if (K[index] <= N / 2)
          goto A;
        else
          goto B;
      } else if (L[index] > R[index]) {
      A:
        for (long long j = (long long)K[index] - 1; j >= (long long)0; j--) {
          if (!f[j]) {
            ans += P[i].first;
            f[j] = 1;
            break;
          }
        }
        for (long long j = (long long)N - 1; j >= (long long)K[index]; j--) {
          if (!f[j]) {
            ans += R[index];
            f[j] = 1;
            break;
          }
        }
      } else {
      B:
        for (long long j = (long long)N - 1; j >= (long long)K[index]; j--) {
          if (!f[j]) {
            ans += P[i].first;
            f[j] = 1;
            break;
          }
        }
        for (long long j = (long long)K[index] - 1; j >= (long long)0; j--) {
          if (!f[j]) {
            ans += L[index];
            f[j] = 1;
            break;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
