#include <bits/stdc++.h>
using namespace std;
const long long zero = 0;
const long long INF = 3000000000000000000;
const int inINF = 1000000000;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
void solve(int N, vector<long long> K, vector<long long> L,
           vector<long long> R) {
  vector<long long> S(N);
  for (int i = 0; i < N; i++) {
    S[i] = L[i] - R[i];
  }
  sort((S).begin(), (S).end(), greater<long long>());
  long long P, Q;
  P = 0;
  Q = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] > 0) {
      P++;
    } else if (S[i] < 0) {
      Q++;
    }
  }
  set<long long> A, B;
  for (int i = 0; i < P + 1; i++) {
    A.insert(N - i);
  }
  for (int i = 0; i < Q + 1; i++) {
    B.insert(N - i);
  }
  vector<bool> ans(N, false);
  int tmp, tt;
  for (int i = 0; i < P; i++) {
    tmp = max(N - K[i] - 1, N - P);
    tt = *A.lower_bound(tmp);
    if (tt != N) {
      A.erase(tt);
      ans[i] = true;
    } else {
      ans[i] = false;
    }
  }
  for (int i = N - 1; i >= N - Q; i--) {
    tmp = max(K[i] + 1, N - Q);
    tt = *B.lower_bound(tmp);
    if (tt != N) {
      B.erase(tt);
      ans[i] = true;
    } else {
      ans[i] = false;
    }
  }
  long long answer = 0;
  for (int i = 0; i < N; i++) {
    answer += R[i];
    if (ans[i]) {
      if (S[i] > 0) {
        answer += S[i];
      }
    } else {
      if (S[i] < 0) {
        answer -= S[i];
      }
    }
  }
  printf("%lld\n", answer);
}
int main() {
  int T;
  int N;
  cin >> T;
  vector<long long> K, L, R;
  for (int i = 0; i < T; i++) {
    cin >> N;
    K.resize(N);
    L.resize(N);
    R.resize(N);
    for (int j = 0; j < N; j++) {
      cin >> K[j] >> L[j] >> R[j];
      K[j]--;
    }
    solve(N, K, L, R);
  }
}
