#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
bool m[N];
vector<int> primes(1, 2);
long double nk[N][N];
void sieve() {
  for (int i = 4; i < N; i += 2) m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i) m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i]) primes.push_back(i);
}
void pascal() {
  for (int i = (0), ThxMK = (N); i < ThxMK; ++i) nk[i][0] = nk[i][i] = 1.0;
  for (int i = (1), ThxMK = (N); i < ThxMK; ++i)
    for (int j = (1), ThxMK = (i); j < ThxMK; ++j)
      nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}
long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }
long long lcm(long long a, long long b) {
  if (a < b) swap(a, b);
  long long c = gcd(a, b);
  a /= c;
  return a * b;
}
int popcount(long long x) { return __builtin_popcountll(x); }
long long poww(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}
long long powm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}
bool isPrime(long long x) {
  if (x == 1) return 0;
  for (auto i : primes) {
    if (i * i > x) return 1;
    if (x % i == 0) return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long a, l, r;
    long long sum = 0;
    priority_queue<pair<long long, long long>> L, R;
    for (int i = (0), ThxMK = (n); i < ThxMK; ++i) {
      cin >> a >> l >> r;
      sum += min(l, r);
      if (l > r)
        L.emplace(-a, l - r);
      else
        R.emplace(a + 1, r - l);
    }
    vector<long long> can(n + 1, 0), con(n + 1, 0);
    int ll = 1, rr = n;
    while (!L.empty()) {
      auto [x, y] = L.top();
      L.pop();
      if (abs(x) < ll) continue;
      sum += y;
      can[ll++] = y;
    }
    while (!R.empty()) {
      auto [x, y] = R.top();
      R.pop();
      if (x > rr) continue;
      sum += y;
      can[rr--] = y;
    }
    for (int i = (0), ThxMK = (n); i < ThxMK; ++i) sum -= min(can[i], con[i]);
    cout << sum << '\n';
  }
}
