#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
const int INF = (1 << 30) - 1;
const int64 INF64 = (1LL << 62) - 1;
template <typename T>
T gcd(T a, T b) {
  if (a < b) return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
template <typename T>
T lcm(const T a, const T b) {
  return a / gcd(a, b) * b;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if (a > b)
    return a = b, true;
  else
    return false;
}
template <typename T>
bool chmax(T& a, const T& b) {
  if (a < b)
    return a = b, true;
  else
    return false;
}
struct S {
  int k;
  int a;
  int b;
};
int64 solve2(vector<S>& s) {
  auto comp = [](S lhs, S rhs) { return lhs.b - lhs.a > rhs.b - rhs.a; };
  priority_queue<S, vector<S>, decltype(comp)> q(comp);
  sort(s.begin(), s.end(), [](S lhs, S rhs) { return lhs.k < rhs.k; });
  for (int i = 0; i < s.size();) {
    int k = s[i].k;
    while (i < s.size() && s[i].k == k) q.push(s[i++]);
    for (int _ = 0; _ < (int)(q.size() - k); ++_) q.pop();
  }
  int64 ans = 0;
  for (auto x : s) ans += x.a;
  while (!q.empty()) ans += q.top().b - q.top().a, q.pop();
  return ans;
}
void solve() {
  int n;
  cin >> n;
  vector<S> left, right;
  left.reserve(n);
  right.reserve(n);
  for (int i = 0; i < (int)(n); ++i) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l > r) {
      left.push_back({k, r, l});
    } else {
      right.push_back({n - k, l, r});
    }
  }
  cout << (solve2(left) + solve2(right)) << '\n';
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < (int)(t); ++i) solve();
  return 0;
}
