#include <bits/stdc++.h>
using namespace std;
size_t f(vector<pair<size_t, size_t>> &x, size_t n) {
  priority_queue<size_t, vector<size_t>, greater<size_t>> candidates;
  priority_queue<pair<size_t, size_t>, vector<pair<size_t, size_t>>,
                 greater<pair<size_t, size_t>>>
      q;
  for (pair<size_t, size_t> p : x) {
    q.push(p);
  }
  size_t current = 0;
  while (!q.empty() && current < n) {
    size_t index = q.top().first;
    if (index >= n) {
      q.pop();
      continue;
    }
    if (index > current) {
      current++;
      while (current < candidates.size()) {
        candidates.pop();
      }
    } else {
      size_t diff = q.top().second;
      q.pop();
      candidates.push(diff);
    }
  }
  size_t ret = 0;
  while (!candidates.empty()) {
    ret += candidates.top();
    candidates.pop();
  }
  return ret;
}
void solve() {
  size_t N;
  cin >> N;
  vector<size_t> K(N);
  vector<size_t> R(N);
  vector<size_t> L(N);
  vector<pair<size_t, size_t>> rr;
  vector<pair<size_t, size_t>> ll;
  size_t ans = 0;
  for (size_t n = 0; n < N; n++) {
    cin >> K[n] >> R[n] >> L[n];
    if (R[n] >= L[n]) {
      rr.push_back(make_pair(K[n] - 1, R[n] - L[n]));
    } else {
      ll.push_back(make_pair((N - 1) - K[n], L[n] - R[n]));
    }
    ans += min(R[n], L[n]);
  }
  ans += f(rr, N);
  ans += f(ll, N);
  cout << ans << endl;
}
int main() {
  size_t T;
  cin >> T;
  for (size_t t = 0; t < T; t++) {
    solve();
  }
}
