#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<long long>> A;
    vector<vector<long long>> B;
    for (long long j = 0; j < N; j++) {
      vector<long long> X(4);
      cin >> X[0] >> X[1] >> X[2];
      if (X[1] - X[2] >= 0) {
        X[3] = X[1] - X[2];
        A.push_back(X);
      } else {
        X[3] = X[2] - X[1];
        B.push_back(X);
      }
    }
    sort(A.begin(), A.end(),
         [](const vector<long long> &alpha, const vector<long long> &beta) {
           return alpha[3] > beta[3];
         });
    sort(A.begin(), A.end(),
         [](const vector<long long> &alpha, const vector<long long> &beta) {
           return alpha[0] < beta[0];
         });
    sort(B.begin(), B.end(),
         [](const vector<long long> &alpha, const vector<long long> &beta) {
           return alpha[3] > beta[3];
         });
    sort(B.begin(), B.end(),
         [](const vector<long long> &alpha, const vector<long long> &beta) {
           return alpha[0] > beta[0];
         });
    long long count = 0;
    long long countb = 0;
    ans[i] = 0;
    for (long long j = 0; j < A.size(); j++) {
      if (count < A[j][0]) {
        count++;
        ans[i] += A[j][1];
      } else {
        ans[i] += A[j][2];
      }
    }
    for (long long j = 0; j < B.size(); j++) {
      if (countb < N - B[j][0]) {
        countb++;
        ans[i] += B[j][2];
      } else {
        ans[i] += B[j][1];
      }
    }
  }
  for (long long i = 0; i < T; i++) {
    cout << ans[i] << endl;
  }
}
