#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long T;
  cin >> T;
  vector<long long> ans(T);
  for (long long i = 0; i < T; i++) {
    long long N;
    cin >> N;
    vector<vector<vector<long long>>> A(N + 1);
    vector<vector<vector<long long>>> B(N + 1);
    for (long long j = 0; j < N; j++) {
      long long a;
      vector<long long> X(3);
      cin >> a >> X[0] >> X[1];
      if (X[0] - X[1] >= 0) {
        X[2] = X[0] - X[1];
        A[a].push_back(X);
      } else {
        X[2] = X[1] - X[0];
        B[a].push_back(X);
      }
    }
    for (long long a = 0; a < N + 1; a++) {
      sort(A[a].begin(), A[a].end(),
           [](const vector<long long> &alpha, const vector<long long> &beta) {
             return alpha[2] > beta[2];
           });
      sort(B[a].begin(), B[a].end(),
           [](const vector<long long> &alpha, const vector<long long> &beta) {
             return alpha[2] > beta[2];
           });
    }
    long long count = 0;
    long long countb = 0;
    ans[i] = 0;
    for (long long a = 0; a < N + 1; a++) {
      for (long long j = 0; j < A[a].size(); j++) {
        if (count < a) {
          count++;
          ans[i] += A[a][j][0];
        } else {
          ans[i] += A[a][j][1];
        }
      }
    }
    for (long long a = 0; a < N + 1; a++) {
      for (long long j = 0; j < B[a].size(); j++) {
        if (countb < N - a) {
          countb++;
          ans[i] += B[a][j][1];
        } else {
          ans[i] += B[a][j][0];
        }
      }
    }
  }
  for (long long i = 0; i < T; i++) {
    cout << ans[i] << endl;
  }
}
