#include <bits/stdc++.h>
using namespace std;
long long T;
long long N, K[1 << 18], L[1 << 18], R[1 << 18];
long long A1[1 << 18], A2[1 << 18];
vector<long long> X[1 << 18];
void init() {
  for (int i = 0; i <= N + 2; i++) A1[i] = 0;
  for (int i = 0; i <= N + 2; i++) A2[i] = 0;
  for (int i = 0; i <= N + 2; i++) X[i].clear();
}
vector<long long> calc(vector<pair<long long, long long>> D) {
  for (int i = 0; i < D.size(); i++) X[D[i].first].push_back(D[i].second);
  priority_queue<long long, vector<long long>, less<long long>> Q;
  vector<long long> R, S;
  for (int i = N; i >= 1; i--) {
    for (long long j : X[i]) Q.push(j);
    if (!Q.empty()) {
      R.push_back(Q.top());
      Q.pop();
    }
  }
  sort(R.begin(), R.end());
  reverse(R.begin(), R.end());
  S.push_back(0);
  for (int i = 0; i < R.size(); i++) S.push_back(S[S.size() - 1] + R[i]);
  return S;
}
long long solve() {
  init();
  long long BASE = 0;
  vector<pair<long long, long long>> V1, V2;
  for (int i = 1; i <= N; i++) {
    BASE += min(L[i], R[i]);
    if (L[i] >= R[i] && K[i] > 0) V1.push_back(make_pair(K[i], L[i] - R[i]));
    if (L[i] < R[i] && K[i] < N) V2.push_back(make_pair(N - K[i], R[i] - L[i]));
  }
  vector<long long> B1 = calc(V1);
  vector<long long> B2 = calc(V2);
  for (int i = 1; i <= N; i++) A1[i] = B1[min(i, (int)B1.size() - 1)];
  for (int i = 1; i <= N; i++) A2[N + 1 - i] = B2[min(i, (int)B2.size() - 1)];
  long long Answer = 0;
  for (int i = 1; i <= N; i++) Answer = max(Answer, A1[i] + A2[i + 1]);
  return BASE + Answer;
}
int main() {
  cin >> T;
  for (int i = 1; i <= T; i++) {
    cin >> N;
    for (int j = 1; j <= N; j++) cin >> K[j] >> L[j] >> R[j];
    cout << solve() << endl;
  }
  return 0;
}
