#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using T = tuple<llong, llong, llong>;
llong solve() {
  llong n;
  priority_queue<T> maxq;
  vector<T> dat;
  llong ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    llong k, l, r;
    cin >> k >> l >> r;
    T a;
    if (l > r) {
      a = T(k, l - r, l);
      ans += r;
      dat.push_back(a);
    } else {
      a = T((n - k), r - l, r);
      ans += l;
      dat.push_back(a);
    }
  }
  sort(dat.begin(), dat.end());
  for (int i = n; i >= 1; i--) {
    while (!dat.empty()) {
      auto a = dat.back();
      auto [k, step, x] = a;
      if (k < i) break;
      dat.pop_back();
      maxq.push(T(step, k, x));
    }
    if (maxq.empty()) continue;
    auto a = maxq.top();
    maxq.pop();
    auto [step, k, x] = a;
    ans += step;
  }
  return ans;
}
int main() {
  llong t;
  cin >> t;
  while (t--) {
    cout << solve() << '\n';
  }
}
