#include <bits/stdc++.h>
using namespace std;
size_t f(vector<pair<size_t, size_t>> &x, size_t n) {
  size_t ret = 0;
  vector<bool> fill(x.size(), false);
  sort(x.begin(), x.end());
  for (pair<size_t, size_t> xx : x) {
    size_t diff = xx.first;
    size_t index = xx.second;
    if (index >= n) {
      continue;
    }
    for (size_t i = min(index, fill.size() - 1); i < fill.size(); i--) {
      if (fill[i]) {
        continue;
      }
      fill[i] = true;
      ret += diff;
      break;
    }
  }
  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(R[n] - L[n], K[n] - 1));
    } else {
      ll.push_back(make_pair(L[n] - R[n], (N - 1) - K[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();
  }
}
