#include <bits/stdc++.h>
const long long MOD = 1000000007ll;
const int MAX = 5100000;
using namespace std;
int inputValue() {
  int a;
  cin >> a;
  return a;
};
void inputArray(int* p, int a) {
  for (int i = 0; i < a; i++) {
    cin >> p[i];
  }
};
void inputVector(vector<int>* p, int a) {
  for (int i = 0; i < a; i++) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}
long long fact[MAX], fact_inv[MAX];
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
long long comb(long long n, long long r) {
  long long t = 1000000;
  fact[0] = 1;
  for (int i = 0; i < t; i++) {
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  }
  fact_inv[t] = power(fact[t], MOD - 2);
  for (int i = 0; i < t; i++) {
    fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
  }
  return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}
bool less_abs(const pair<int, long long>& firstElof,
              const pair<int, long long>& secondElof) {
  return abs(firstElof.second) >= abs(secondElof.second);
}
void solve() {
  int N;
  cin >> N;
  long long ans = 0;
  int i, j;
  int k;
  long long L, R;
  vector<pair<int, long long> > K;
  int chk[N + 1];
  for (int i = 0; i < (N); i++) {
    chk[i + 1] = 0;
  }
  for (i = 0; i < N; i++) {
    cin >> k >> L >> R;
    ans += L;
    if (k < N && L != R) {
      K.push_back(make_pair(k, L - R));
    }
  }
  sort(K.begin(), K.end(), less_abs);
  for (i = 0; i < K.size(); i++) {
    bool bOK = false;
    if (K[i].second > 0) {
      for (j = K[i].first; j >= 1; j--) {
        if (chk[j] == 0) {
          chk[j]++;
          bOK = true;
          break;
        }
      }
      if (bOK == false) {
        ans -= K[i].second;
      }
    } else {
      for (j = K[i].first + 1; j <= N; j++) {
        if (chk[j] == 0) {
          chk[j]++;
          bOK = true;
          break;
        }
      }
      if (bOK == true) {
        ans -= K[i].second;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int i = 1; i <= T; i++) {
    solve();
  }
  return 0;
}
