#include <bits/stdc++.h>
using namespace std;
long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);
int main() {
  int T, N, k, l, r;
  long long int total;
  cin >> T;
  for (int t = 0; t < T; t++) {
    total = 0;
    cin >> N;
    int K[N][3];
    priority_queue<int, vector<int>, greater<int>> H;
    priority_queue<int, vector<int>, greater<int>> T;
    for (int n = 0; n < N; n++) {
      cin >> K[n][0] >> K[n][1] >> K[n][2];
      K[n][0] = K[n][0] - 1;
      total = total + min(K[n][1], K[n][2]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[j][0] == i & K[j][1] > K[j][2]) {
          H.push(K[j][1] - K[j][2]);
        }
      }
      while (H.size() > i + 1) {
        H.pop();
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (K[j][0] == N - 1 - i & K[j][1] < K[j][2]) {
          T.push(K[j][2] - K[j][1]);
        }
      }
      while (T.size() > i) {
        T.pop();
      }
    }
    while (!H.empty()) {
      total = total + H.top();
      H.pop();
    }
    while (!T.empty()) {
      total = total + T.top();
      T.pop();
    }
    cout << total << endl;
  }
  return 0;
}
long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;
  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }
    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}
